Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
risenW committed Jan 19, 2022
2 parents d267db6 + 0d33e34 commit d3df3a3
Show file tree
Hide file tree
Showing 22 changed files with 936 additions and 528 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ yarn add danfojs
For use directly in HTML files, you can add the latest script tag from [JsDelivr](https://www.jsdelivr.com/package/npm/danfojs) to your HTML file:

```html
<script src="https://cdn.jsdelivr.net/npm/[email protected].1/lib/bundle.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected].2/lib/bundle.js"></script>
```
See all available versions [here](https://www.jsdelivr.com/package/npm/danfojs)

Expand All @@ -85,7 +85,7 @@ See all available versions [here](https://www.jsdelivr.com/package/npm/danfojs)
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<script src="https://cdn.jsdelivr.net/npm/[email protected].1/lib/bundle.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected].2/lib/bundle.js"></script>

<title>Document</title>
</head>
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"name": "danfo",
"version": "1.0.2",
"private": true,
"workspaces": [
"danfojs-node/**",
Expand Down
60 changes: 41 additions & 19 deletions src/danfojs-base/core/datetime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,14 @@ export default class TimeSeries implements DateTime {
* Returns the month, in local time.
* @example
* ```
* import { Dataframe } from "danfojs-node"
* import { Series } from "danfojs-node"
* const data = [
* "2019-01-01",
* "2019-02-01",
* "2019-03-01",
* "2019-04-01",
* ]
* const df = new Dataframe(data)
* const df = new Series(data)
* const dfNew = df.dt.month()
* console.log(dfNew.values)
* // [1, 2, 3, 4]
Expand All @@ -68,19 +68,19 @@ export default class TimeSeries implements DateTime {
* Returns the day of the week, in local time
* @example
* ```
* import { Dataframe } from "danfojs-node"
* import { Series } from "danfojs-node"
* const data = [
* "2019-01-01",
* "2019-02-01",
* "2019-03-01",
* "2019-04-01",
* ]
* const df = new Dataframe(data)
* const df = new Series(data)
* const dayOfWeek = df.dt.dayOfWeek()
* console.log(dayOfWeek.values)
* ```
*/
dayOfWeek() {
dayOfWeek() {
const newValues = this.$dateObjectArray.map(date => date.getDay())
return new Series(newValues);
}
Expand All @@ -89,14 +89,14 @@ export default class TimeSeries implements DateTime {
* Returns the year, in local time
* @example
* ```
* import { Dataframe } from "danfojs-node"
* import { Series } from "danfojs-node"
* const data = [
* "2019-01-01",
* "2019-02-01",
* "2021-03-01",
* "2020-04-01",
* ]
* const df = new Dataframe(data)
* const df = new Series(data)
* const year = df.dt.year()
* console.log(year.values)
* // [2019, 2019, 2021, 2020]
Expand All @@ -111,14 +111,14 @@ export default class TimeSeries implements DateTime {
* Returns the name of the month, in local time
* @example
* ```
* import { Dataframe } from "danfojs-node"
* import { Series } from "danfojs-node"
* const data = [
* "2019-01-01",
* "2019-02-01",
* "2021-03-01",
* "2020-04-01",
* ]
* const df = new Dataframe(data)
* const df = new Series(data)
* const monthName = df.dt.monthName().values
* console.log(monthName)
* // ["January", "February", "March", "April"]
Expand All @@ -133,14 +133,14 @@ export default class TimeSeries implements DateTime {
* Returns the name of the day, of the week, in local time
* @example
* ```
* import { Dataframe } from "danfojs-node"
* import { Series } from "danfojs-node"
* const data = [
* "2019-01-01",
* "2019-02-01",
* "2021-03-01",
* "2020-04-01",
* ]
* const df = new Dataframe(data)
* const df = new Series(data)
* const dayOfWeekName = df.dt.dayOfWeekName().values
* console.log(dayOfWeekName)
* ```
Expand All @@ -154,14 +154,14 @@ export default class TimeSeries implements DateTime {
* Returns the day of the month, in local time
* @example
* ```
* import { Dataframe } from "danfojs-node"
* import { Series } from "danfojs-node"
* const data = [
* "2019-01-01",
* "2019-02-05",
* "2021-03-02",
* "2020-04-01",
* ]
* const df = new Dataframe(data)
* const df = new Series(data)
* const dayOfMonth = df.dt.dayOfMonth().values
* console.log(dayOfMonth)
* // [1, 5, 2, 1]
Expand All @@ -176,14 +176,14 @@ export default class TimeSeries implements DateTime {
* Returns the hour of the day, in local time
* @example
* ```
* import { Dataframe } from "danfojs-node"
* import { Series } from "danfojs-node"
* const data = [
* "2019-01-01",
* "2019-02-05",
* "2021-03-02",
* "2020-04-01",
* ]
* const df = new Dataframe(data)
* const df = new Series(data)
* const hour = df.dt.hour().values
* console.log(hour)
* // [0, 0, 0, 0]
Expand All @@ -198,14 +198,14 @@ export default class TimeSeries implements DateTime {
* Returns the second of the day, in local time
* @example
* ```
* import { Dataframe } from "danfojs-node"
* import { Series } from "danfojs-node"
* const data = [
* "2019-01-01",
* "2019-02-05",
* "2021-03-02",
* "2020-04-01",
* ]
* const df = new Dataframe(data)
* const df = new Series(data)
* const second = df.dt.second().values
* console.log(second)
* ```
Expand All @@ -219,14 +219,14 @@ export default class TimeSeries implements DateTime {
* Returns the minute of the day, in local time
* @example
* ```
* import { Dataframe } from "danfojs-node"
* import { Series } from "danfojs-node"
* const data = [
* "2019-01-01",
* "2019-02-05",
* "2021-03-02",
* "2020-04-01",
* ]
* const df = new Dataframe(data)
* const df = new Series(data)
* const minute = df.dt.minute().values
* console.log(minute)
* ```
Expand All @@ -236,6 +236,28 @@ export default class TimeSeries implements DateTime {
return new Series(newValues);
}

/**
* Returns the Date as JavaScript standard Date object
* @example
* ```
* import { Series } from "danfojs-node"
* const data = [
* "2019-01-01",
* "2019-02-05",
* "2021-03-02",
* "2020-04-01",
* ]
*
* const df = new Series(data)
* const date = df.dt.toDate().values
* console.log(date)
* ```
*/
date() {
const newValues = this.$dateObjectArray.map(date => date.toLocaleString())
return new Series(newValues);
}

}

export const toDateTime = (data: Series | ArrayType1D) => {
Expand Down
49 changes: 46 additions & 3 deletions src/danfojs-base/core/frame.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1747,8 +1747,8 @@ export default class DataFrame extends NDframe implements DataFrameInterface {
options?: { inplace?: boolean, atIndex?: number | string }
): DataFrame | void {
let { inplace, atIndex } = { inplace: false, atIndex: this.columns.length, ...options };
if (typeof atIndex === "string" ) {
if (!(this.columns.includes(atIndex))){
if (typeof atIndex === "string") {
if (!(this.columns.includes(atIndex))) {
throw new Error(`${atIndex} not a column`)
}
atIndex = this.columns.indexOf(atIndex)
Expand Down Expand Up @@ -2762,7 +2762,7 @@ export default class DataFrame extends NDframe implements DataFrameInterface {
inplace?: boolean
}
): DataFrame
rename(
rename(
mapper: {
[index: string | number]: string | number
},
Expand Down Expand Up @@ -3332,4 +3332,47 @@ export default class DataFrame extends NDframe implements DataFrameInterface {
return toExcelNode(this, options as ExcelOutputOptionsNode)
}
}

/**
* Access a single value for a row/column pair by integer position.
* Similar to {@link iloc}, in that both provide integer-based lookups.
* Use iat if you only need to get or set a single value in a DataFrame.
* @param row Row index of the value to access.
* @param column Column index of the value to access.
* @example
* ```
* const df = new DataFrame([[1, 2], [3, 4]], { columns: ['A', 'B']})
* df.iat(0, 0) // 1
* df.iat(0, 1) // 2
* df.iat(1, 0) // 3
* ```
*/
iat(row: number, column: number): string | number | boolean | undefined {
if (typeof row === 'string' || typeof column === 'string') {
throw new Error('ParamError: row and column index must be an integer. Use .at to get a row or column by label.')
}

return (this.values as ArrayType2D)[row][column]
}

/**
* Access a single value for a row/column label pair.
* Similar to {@link loc}, in that both provide label-based lookups.
* Use at if you only need to get or set a single value in a DataFrame.
* @param row Row index of the value to access.
* @param column Column label of the value to access.
* @example
* ```
* const df = new DataFrame([[1, 2], [3, 4]], { columns: ['A', 'B']})
* df.at(0,'A') // 1
* df.at(1, 'A') // 3
* df.at(1, 'B') // 4
* ```
*/
at(row: string | number, column: string): string | number | boolean | undefined {
if (typeof column !== 'string') {
throw new Error('ParamError: column index must be a string. Use .iat to get a row or column by index.')
}
return (this.values as ArrayType2D)[this.index.indexOf(row)][this.columns.indexOf(column)]
}
}
56 changes: 52 additions & 4 deletions src/danfojs-base/core/series.ts
Original file line number Diff line number Diff line change
Expand Up @@ -674,9 +674,10 @@ export default class Series extends NDframe implements SeriesInterface {
* //output [ 1.23, 2.4, 3.12, 4.12, 5.12 ]
* ```
*/
round(dp?: number, options?: { inplace?: boolean }): Series
round(dp = 1, options?: { inplace?: boolean }): Series | void {
const { inplace } = { inplace: false, ...options }

if (dp === undefined) dp = 1;
const newValues = utils.round(this.values as number[], dp, true);

if (inplace) {
Expand Down Expand Up @@ -816,24 +817,26 @@ export default class Series extends NDframe implements SeriesInterface {
const { ascending, inplace, } = { ascending: true, inplace: false, ...options }

let sortedValues = [];
let sortedIndex = []
const rangeIdx = utils.range(0, this.index.length - 1);
let sortedIdx = utils.sortArrayByIndex(rangeIdx, this.values, this.dtypes[0]);

for (let indx of sortedIdx) {
sortedValues.push(this.values[indx])
sortedIndex.push(this.index[indx])
}

if (ascending) {
sortedValues = sortedValues.reverse();
sortedIdx = sortedIdx.reverse();
sortedIndex = sortedIndex.reverse();
}

if (inplace) {
this.$setValues(sortedValues as ArrayType1D)
this.$setIndex(sortedIdx);
this.$setIndex(sortedIndex);
} else {
const sf = new Series(sortedValues, {
index: sortedIdx,
index: sortedIndex,
dtypes: this.dtypes,
config: this.config
});
Expand Down Expand Up @@ -1828,6 +1831,11 @@ export default class Series extends NDframe implements SeriesInterface {
* //output [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13 ]
* ```
*/
append(
newValue: string | number | boolean | Series | ArrayType1D,
index: Array<number | string> | number | string,
options?: { inplace?: boolean }
): Series
append(
newValue: string | number | boolean | Series | ArrayType1D,
index: Array<number | string> | number | string,
Expand Down Expand Up @@ -2239,4 +2247,44 @@ export default class Series extends NDframe implements SeriesInterface {
return toExcelNode(this, options as ExcelOutputOptionsNode)
}
}

/**
* Access a single value for a row index.
* Similar to iloc, in that both provide index-based lookups.
* Use iat if you only need to get or set a single value in a Series.
* @param row Row index of the value to access.
* @example
* ```
* const sf = new Series([1, 2, 3, 4, 5])
* sf.iat(0) //returns 1
* sf.iat(1) //returns 2
* sf.iat(2) //returns 3
* ```
*/
iat(row: number): number | string | boolean | undefined {
if (typeof row === 'string') {
throw new Error('ParamError: row index must be an integer. Use .at to get a row by label.')
}
return (this.values as ArrayType1D)[row];
}

/**
* Access a single value for a row label.
* Similar to loc, in that both provide label-based lookups.
* Use at if you only need to get or set a single value in a Series.
* @param row Row label of the value to access.
* @example
* ```
* const sf = new Series([1, 2, 3, 4, 5, 6], { index: ['A', 'B', 'C', 'D', 'E', 'F'] })
* sf.at('A') //returns 1
* sf.at('B') //returns 2
* sf.at('C') //returns 3
* ```
*/
at(row: string): number | string | boolean | undefined {
if (typeof row !== 'string') {
throw new Error('ParamError: row index must be a string. Use .iat to get a row by index.')
}
return (this.values as ArrayType1D)[this.index.indexOf(row)];
}
}
2 changes: 1 addition & 1 deletion src/danfojs-base/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import merge from "./transformers/merge"
import dateRange from "./core/daterange"
import tensorflow from "./shared/tensorflowlib"

const __version = "1.0.1";
const __version = "1.0.2";

export {
NDframe,
Expand Down

0 comments on commit d3df3a3

Please sign in to comment.