Skip to content

Commit 93a9344

Browse files
committed
Add iat and at methods to Series
1 parent c119442 commit 93a9344

File tree

5 files changed

+156
-6
lines changed

5 files changed

+156
-6
lines changed

src/danfojs-base/core/frame.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3336,7 +3336,7 @@ export default class DataFrame extends NDframe implements DataFrameInterface {
33363336
/**
33373337
* Access a single value for a row/column pair by integer position.
33383338
* Similar to {@link iloc}, in that both provide integer-based lookups.
3339-
* Use iat if you only need to get or set a single value in a DataFrame or Series.
3339+
* Use iat if you only need to get or set a single value in a DataFrame.
33403340
* @param row Row index of the value to access.
33413341
* @param column Column index of the value to access.
33423342
* @example
@@ -3347,7 +3347,7 @@ export default class DataFrame extends NDframe implements DataFrameInterface {
33473347
* df.iat(1, 0) // 3
33483348
* ```
33493349
*/
3350-
iat(row: number, column: number): string | number | boolean {
3350+
iat(row: number, column: number): string | number | boolean | undefined {
33513351
if(typeof row === 'string' || typeof column === 'string') {
33523352
throw new Error('ParamError: row and column index must be an integer. Use .at to get a row or column by label.')
33533353
}
@@ -3358,7 +3358,7 @@ export default class DataFrame extends NDframe implements DataFrameInterface {
33583358
/**
33593359
* Access a single value for a row/column label pair.
33603360
* Similar to {@link loc}, in that both provide label-based lookups.
3361-
* Use at if you only need to get or set a single value in a DataFrame or Series.
3361+
* Use at if you only need to get or set a single value in a DataFrame.
33623362
* @param row Row index of the value to access.
33633363
* @param column Column label of the value to access.
33643364
* @example
@@ -3369,7 +3369,7 @@ export default class DataFrame extends NDframe implements DataFrameInterface {
33693369
* df.at(1, 'B') // 4
33703370
* ```
33713371
*/
3372-
at(row: string | number, column: string): string | number | boolean {
3372+
at(row: string | number, column: string): string | number | boolean | undefined {
33733373
if(typeof column !== 'string') {
33743374
throw new Error('ParamError: column index must be a string. Use .iat to get a row or column by index.')
33753375
}

src/danfojs-base/core/series.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2239,4 +2239,44 @@ export default class Series extends NDframe implements SeriesInterface {
22392239
return toExcelNode(this, options as ExcelOutputOptionsNode)
22402240
}
22412241
}
2242+
2243+
/**
2244+
* Access a single value for a row index.
2245+
* Similar to iloc, in that both provide index-based lookups.
2246+
* Use iat if you only need to get or set a single value in a Series.
2247+
* @param row Row index of the value to access.
2248+
* @example
2249+
* ```
2250+
* const sf = new Series([1, 2, 3, 4, 5])
2251+
* sf.iat(0) //returns 1
2252+
* sf.iat(1) //returns 2
2253+
* sf.iat(2) //returns 3
2254+
* ```
2255+
*/
2256+
iat(row: number): number | string | boolean | undefined {
2257+
if(typeof row === 'string') {
2258+
throw new Error('ParamError: row index must be an integer. Use .at to get a row by label.')
2259+
}
2260+
return (this.values as ArrayType1D)[row];
2261+
}
2262+
2263+
/**
2264+
* Access a single value for a row label.
2265+
* Similar to loc, in that both provide label-based lookups.
2266+
* Use at if you only need to get or set a single value in a Series.
2267+
* @param row Row label of the value to access.
2268+
* @example
2269+
* ```
2270+
* const sf = new Series([1, 2, 3, 4, 5, 6], { index: ['A', 'B', 'C', 'D', 'E', 'F'] })
2271+
* sf.at('A') //returns 1
2272+
* sf.at('B') //returns 2
2273+
* sf.at('C') //returns 3
2274+
* ```
2275+
*/
2276+
at(row: string): number | string | boolean | undefined {
2277+
if(typeof row !== 'string') {
2278+
throw new Error('ParamError: row index must be a string. Use .iat to get a row by index.')
2279+
}
2280+
return (this.values as ArrayType1D)[this.index.indexOf(row)];
2281+
}
22422282
}

src/danfojs-base/shared/types.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,8 @@ export interface SeriesInterface extends NDframeInterface {
184184
toCSV(options?: CsvOutputOptionsBrowser): string | void
185185
toJSON(options?: JsonOutputOptionsBrowser): object | void
186186
toExcel(options?: ExcelOutputOptionsBrowser): void
187+
iat(index: number): number | string | boolean | undefined
188+
at(index: string | number): number | string | boolean | undefined
187189
}
188190

189191
//Start of DataFrame class types
@@ -327,8 +329,8 @@ export interface DataFrameInterface extends NDframeInterface {
327329
toCSV(options?: CsvOutputOptionsBrowser): string | void
328330
toJSON(options?: JsonOutputOptionsBrowser): object | void
329331
toExcel(options?: ExcelOutputOptionsBrowser): void
330-
iat(row: number, column: number): string | number | boolean
331-
at(row: string | number, column: string): string | number | boolean
332+
iat(row: number, column: number): number | string | boolean | undefined
333+
at(row: string | number, column: string): number | string | boolean | undefined
332334
}
333335

334336
export interface DateTime {

src/danfojs-browser/tests/core/series.test.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1583,4 +1583,58 @@ describe("Series Functions", () => {
15831583
});
15841584

15851585
});
1586+
1587+
describe("iat", function () {
1588+
it("iat works on Series", function () {
1589+
const data = [ 1, 2, 3, 4 ];
1590+
const index = [ "a", "b", "c", "d" ];
1591+
const df = new dfd.Series(data, { index });
1592+
assert.equal(df.iat(0), 1);
1593+
assert.equal(df.iat(1), 2);
1594+
assert.equal(df.iat(2), 3);
1595+
});
1596+
it("iat can return undefined", function () {
1597+
const data = [ 1, undefined, null, NaN ];
1598+
const df = new dfd.Series(data);
1599+
assert.equal(df.iat(1), undefined);
1600+
assert.equal(df.iat(2), null);
1601+
/* @ts-ignore */
1602+
assert.equal(isNaN(df.iat(3)), true);
1603+
});
1604+
it("throws error on string indices", function () {
1605+
const data = [ 1, 2, 3, 4 ];
1606+
const index = [ "a", "b", "c", "d" ];
1607+
const df = new dfd.Series(data, { index });
1608+
/* @ts-ignore */
1609+
assert.throws(function () { df.iat("A"); }, Error, "ParamError: row index must be an integer. Use .at to get a row by label.");
1610+
});
1611+
});
1612+
1613+
describe("at", function () {
1614+
it("at works on Series", function () {
1615+
const data = [ 1, 2, 3, 4 ];
1616+
const index = [ "a", "b", "c", "d" ];
1617+
const df = new dfd.Series(data, { index });
1618+
assert.equal(df.at("a"), 1);
1619+
assert.equal(df.at("b"), 2);
1620+
assert.equal(df.at("c"), 3);
1621+
});
1622+
it("at can return undefined", function () {
1623+
const data = [ 1, undefined, null, NaN ];
1624+
const index = [ "a", "b", "c", "d" ];
1625+
const df = new dfd.Series(data, { index });
1626+
assert.equal(df.at("b"), undefined);
1627+
assert.equal(df.at("c"), null);
1628+
/* @ts-ignore */
1629+
assert.equal(isNaN(df.at("d")), true);
1630+
});
1631+
it("throws error on string indices", function () {
1632+
const data = [ 1, 2, 3, 4 ];
1633+
const index = [ "a", "b", "c", "d" ];
1634+
const df = new dfd.Series(data, { index });
1635+
/* @ts-ignore */
1636+
assert.throws(function () { df.at(0); }, Error, "ParamError: row index must be a string. Use .iat to get a row by index.");
1637+
});
1638+
1639+
});
15861640
});

src/danfojs-node/test/core/series.test.ts

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1591,4 +1591,58 @@ describe("Series Functions", () => {
15911591
assert.deepEqual(sf.and(data2).index, expected);
15921592
});
15931593
});
1594+
1595+
describe("iat", function () {
1596+
it("iat works on Series", function () {
1597+
const data = [1, 2, 3, 4];
1598+
const index = ["a", "b", "c", "d"];
1599+
const df = new Series(data, { index });
1600+
assert.equal(df.iat(0), 1);
1601+
assert.equal(df.iat(1), 2);
1602+
assert.equal(df.iat(2), 3);
1603+
});
1604+
it("iat can return undefined", function () {
1605+
const data = [1, undefined, null, NaN];
1606+
const df = new Series(data);
1607+
assert.equal(df.iat(1), undefined);
1608+
assert.equal(df.iat(2), null);
1609+
/* @ts-ignore */
1610+
assert.equal(isNaN(df.iat(3)), true);
1611+
});
1612+
it("throws error on string indices", function () {
1613+
const data = [1, 2, 3, 4];
1614+
const index = ["a", "b", "c", "d"];
1615+
const df = new Series(data, { index });
1616+
/* @ts-ignore */
1617+
assert.throws(function () { df.iat("A"); }, Error, "ParamError: row index must be an integer. Use .at to get a row by label.");
1618+
});
1619+
})
1620+
1621+
describe("at", function () {
1622+
it("at works on Series", function () {
1623+
const data = [1, 2, 3, 4];
1624+
const index = ["a", "b", "c", "d"];
1625+
const df = new Series(data, { index });
1626+
assert.equal(df.at("a"), 1);
1627+
assert.equal(df.at("b"), 2);
1628+
assert.equal(df.at("c"), 3);
1629+
});
1630+
it("at can return undefined", function () {
1631+
const data = [1, undefined, null, NaN];
1632+
const index = ["a", "b", "c", "d"];
1633+
const df = new Series(data, { index });
1634+
assert.equal(df.at("b"), undefined);
1635+
assert.equal(df.at("c"), null);
1636+
/* @ts-ignore */
1637+
assert.equal(isNaN(df.at("d")), true);
1638+
});
1639+
it("throws error on string indices", function () {
1640+
const data = [1, 2, 3, 4];
1641+
const index = ["a", "b", "c", "d"];
1642+
const df = new Series(data, { index });
1643+
/* @ts-ignore */
1644+
assert.throws(function () { df.at(0); }, Error, "ParamError: row index must be a string. Use .iat to get a row by index.");
1645+
});
1646+
1647+
});
15941648
})

0 commit comments

Comments
 (0)