loc 使用 DataFrame 自己的索引在 DataFrame 上失败?
loc fails on a DataFrame using the DataFrame's own index?
我有一个带有 DateTime 索引的 DataFrame,其中有许多重复的索引标签(即具有相同日期时间的行)。我想查看具有相同日期时间的行。所以我有以下
utimes = pd.unique(data.index.tolist())
for time in utimes:
data_now = data.loc[time]
# Do some processing on the data_now
失败并出现示例错误:KeyError 'the label [2015-02-05 21:54:00+00:00] is not in the [index]'
只是为了检查这不是创建 utimes 的问题,失败了
data.loc[data.index[0]]
同样的错误信息。怎么会这样?这是索引的样子
> data.index
<class 'pandas.tseries.index.DatetimeIndex'>
[2015-02-05 21:54:00+00:00, ..., 2015-02-05 23:24:00+00:00]
Length: 457, Freq: None, Timezone: UTC
和
> data.index[0]
Timestamp('2015-02-05 22:24:00+0000', tz='UTC')
知道为什么我不能将 .loc 与 data_frame 自己的索引一起使用吗??
看起来 pd.unique
不符合 datetime64
数据类型:
In [11]: df.index
Out[11]:
<class 'pandas.tseries.index.DatetimeIndex'>
[2015-02-05 22:24:00+00:00]
Length: 1, Freq: None, Timezone: UTC
In [12]: pd.unique(df.index)
Out[12]: array([1423175040000000000L], dtype=object)
现在(直到这个错误在 pandas 中被修复)你可以把它包装在一个 to_datetime
调用中:
In [13]: pd.to_datetime(pd.unique(df.index))
Out[13]:
<class 'pandas.tseries.index.DatetimeIndex'>
[2015-02-05 22:24:00]
Length: 1, Freq: None, Timezone: None
或者,更干净,您可以使用独特的方法 DatetimeIndex:
In [14]: df.index.unique()
Out[14]:
<class 'pandas.tseries.index.DatetimeIndex'>
[2015-02-05 22:24:00+00:00]
Length: 1, Freq: None, Timezone: UTC
我有一个带有 DateTime 索引的 DataFrame,其中有许多重复的索引标签(即具有相同日期时间的行)。我想查看具有相同日期时间的行。所以我有以下
utimes = pd.unique(data.index.tolist())
for time in utimes:
data_now = data.loc[time]
# Do some processing on the data_now
失败并出现示例错误:KeyError 'the label [2015-02-05 21:54:00+00:00] is not in the [index]'
只是为了检查这不是创建 utimes 的问题,失败了
data.loc[data.index[0]]
同样的错误信息。怎么会这样?这是索引的样子
> data.index
<class 'pandas.tseries.index.DatetimeIndex'>
[2015-02-05 21:54:00+00:00, ..., 2015-02-05 23:24:00+00:00]
Length: 457, Freq: None, Timezone: UTC
和
> data.index[0]
Timestamp('2015-02-05 22:24:00+0000', tz='UTC')
知道为什么我不能将 .loc 与 data_frame 自己的索引一起使用吗??
看起来 pd.unique
不符合 datetime64
数据类型:
In [11]: df.index
Out[11]:
<class 'pandas.tseries.index.DatetimeIndex'>
[2015-02-05 22:24:00+00:00]
Length: 1, Freq: None, Timezone: UTC
In [12]: pd.unique(df.index)
Out[12]: array([1423175040000000000L], dtype=object)
现在(直到这个错误在 pandas 中被修复)你可以把它包装在一个 to_datetime
调用中:
In [13]: pd.to_datetime(pd.unique(df.index))
Out[13]:
<class 'pandas.tseries.index.DatetimeIndex'>
[2015-02-05 22:24:00]
Length: 1, Freq: None, Timezone: None
或者,更干净,您可以使用独特的方法 DatetimeIndex:
In [14]: df.index.unique()
Out[14]:
<class 'pandas.tseries.index.DatetimeIndex'>
[2015-02-05 22:24:00+00:00]
Length: 1, Freq: None, Timezone: UTC