pandas 使用日期时间对象重新索引 DataFrame

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10943478/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 15:44:07  来源:igfitidea点击:

pandas reindex DataFrame with datetime objects

pythondataframepandasreindex

提问by BFTM

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

是否可以DataFrame使用由日期时间对象组成的列重新索引熊猫?

I have a DataFrame dfwith the following columns:

我有一个包含df以下列的 DataFrame :

Int64Index: 19610 entries, 0 to 19609
Data columns:
cntr                  19610  non-null values  #int
datflt                19610  non-null values  #float
dtstamp               19610  non-null values  #datetime object
DOYtimestamp          19610  non-null values  #float
dtypes: int64(1), float64(2), object(1)

I can reindex the dfeasily along DOYtimestampwith: df.reindex(index=df.dtstamp)and DOYtimestamphas the following values:

我可以df轻松地重新索引DOYtimestamp与:df.reindex(index=df.dtstamp)DOYtimestamp具有以下值:

>>> df['DOYtimestamp'].values
    array([ 153.76252315,  153.76253472,  153.7625463 , ...,  153.98945602,
    153.98946759,  153.98947917])

but I'd like to reindex the DataFrame along dtstampwhich is made up of datetime objects so that I generate different timestamps directly from the index. The dtstampcolumn has values which look like:

但我想重新索引dtstamp由 datetime 对象组成的 DataFrame ,以便我直接从索引生成不同的时间戳。该dtstamp列具有如下所示的值:

 >>> df['dtstamp'].values
     array([2012-06-02 18:18:02, 2012-06-02 18:18:03, 2012-06-02 18:18:04, ...,
     2012-06-02 23:44:49, 2012-06-02 23:44:50, 2012-06-02 23:44:51], 
     dtype=object)

When I try and reindex dfalong dtstampI get the following:

当我尝试重新索引时dfdtstamp我得到以下信息:

>>> df.reindex(index=df.dtstamp)
    TypeError: can't compare datetime.datetime to long

I'm just not sure what I need to do get the index to be of a datetime type. Any thoughts?

我只是不确定我需要做什么才能使索引成为日期时间类型。有什么想法吗?

回答by BrenBarn

It sounds like you don't want reindex. Somewhat confusingly reindexis 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 NaNvalues, since there are no rows with indices 8, 9, or 10.

听起来你不想重新索引。reindex确切地说,有点令人困惑的是不是为了定义新索引;相反,它查找具有指定索引的行。因此,如果您有一个带有 index 的 DataFrame [0, 1, 2],那么执行 areindex([2, 1, 0])将以相反的顺序返回行。执行类似reindex([8, 9, 10])操作不会为行创建新索引;相反,它将返回一个带有NaN值的 DataFrame ,因为没有索引为 8、9 或 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'].

看起来您想要的只是保留相同的行,但为它们创建一个全新的索引。为此,您可以直接分配给索引。所以尝试做df.index = df['dtstamp']