pandas reindex DataFrame with datetime objects

前端 未结 1 448
春和景丽
春和景丽 2021-02-07 11:58

Is it possible to reindex a pandas DataFrame using a column made up of datetime objects?

I have a DataFrame df with the following columns:

相关标签:
1条回答
  • 2021-02-07 12:57

    It sounds like you don't want reindex. Somewhat confusingly reindex is not for defining a new index, exactly; rather, it looks for rows that have the specified indices. So if you have a DataFrame with index [0, 1, 2], then doing a reindex([2, 1, 0]) will return the rows in reverse order. Doing something like reindex([8, 9, 10]) does not make a new index for the rows; rather, it will return a DataFrame with NaN values, since there are no rows with indices 8, 9, or 10.

    It seems like what you want is to just keep the same rows, but make a totally new index for them. For that you can just assign to the index directly. So try doing df.index = df['dtstamp'].

    0 讨论(0)
提交回复
热议问题