Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

reindex the dataframe #10

Closed
erictamhk opened this issue Jul 17, 2018 · 6 comments
Closed

reindex the dataframe #10

erictamhk opened this issue Jul 17, 2018 · 6 comments

Comments

@erictamhk
Copy link

i want to have a function that can reindex the dataframe
something like the pandas.DataFrame.reindex

@ashleydavis
Copy link
Member

I'm not sure off the top of my head, I'll have to look up the Pandas reindex function to remember how it works. I'll get back to you in a couple of days.

Maybe you could describe to me the behavior you'd like to see in DataForge?

@erictamhk
Copy link
Author

erictamhk commented Jul 19, 2018

i implement this function by using javascript, i don't know am i do it right, but it work.
i find that the DataFrame.at() is very slow to get a row of data.

`dataForge.DataFrame.prototype.reindex = function(new_index, fill_value = null) {
let newArray = ["tempIndex"].concat(this.getColumnNames());
let pairs = this.toPairs();
let newIndexArray = new_index.toArray();
const dfIndexArray = this.getIndex().toArray();

const filterPairs = pairs.reduce(function(a, e, i) {
if (newIndexArray.indexOf(e[0]) >= 0) {
let obj = e[1];
let idx = e[0];
let newObj = {};
newArray.forEach(val => {
if (val === "tempIndex") {
newObj[val] = idx;
} else {
newObj[val] = obj[val];
}
});
a.push(newObj);
}
return a;
}, []);

const emptyPairs = newIndexArray.reduce(function(a, e, i) {
if (dfIndexArray.indexOf(e) === -1) {
let newObj = {};
newArray.forEach(val => {
if (val === "tempIndex") {
newObj[val] = e;
} else {
newObj[val] = fill_value;
}
});
a.push(newObj);
}
return a;
}, []);`

@ashleydavis
Copy link
Member

Reindex won't make at any faster.

Currently at doesn't even use the index. I'll re-write the function at some point so that uses the index.

@shajeeck
Copy link

@ashleydavis as you mentioned at doesn't use index, what is the function which uses index to return the row?

@shajeeck
Copy link

@ashleydavis
Copy link
Member

The at function takes an index and returns the first value at that index.

The at function is not optimised, so it is quite slow (this can be fixed of course),

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants