我正在尝试使用以下代码获取每个组的最新实例。它做我想要的,除了时间戳被转换为 numpy.datetime 并且日期减去一天。这似乎不是正确的行为。这是一个错误还是我错过了什么。
In [37]: df
Out[37]:
ticker currency date
0 AACE NaN NaT
1 AAP US Dollar 2012-12-29 00:00:00
2 AAP US Dollar 2013-04-20 00:00:00
3 AAP US Dollar 2013-07-13 00:00:00
4 ABBEY British Pound 2012-12-31 00:00:00
5 ABBEY British Pound 2013-03-30 00:00:00
6 ABBEY British Pound 2013-06-30 00:00:00
7 ABBNVX NaN NaT
8 ABBV US Dollar 2012-12-31 00:00:00
9 ABBV US Dollar 2013-03-31 00:00:00
10 ABBV US Dollar 2013-06-30 00:00:00
In [38]: df.date[3]
Out[38]: Timestamp('2013-07-13 00:00:00', tz=None)
In [39]: df.groupby('ticker').last()
Out[39]:
currency date ticker
AACE NaN NaN
AAP US Dollar 2013-07-12T17:00:00.000000000-0700
ABBEY British Pound 2013-06-29T17:00:00.000000000-0700
ABBNVX NaN NaN
ABBV US Dollar 2013-06-29T17:00:00.000000000-0700
In [40]: df.groupby('ticker').last().date[1]
Out[40]: numpy.datetime64('2013-07-12T17:00:00.000000000-0700')
In [41]:
编辑:
我没有原始示例,但这是另一个复制相同行为的示例。
In [57]: df
Out[57]:
ticker currency date
3227 WWW US Dollar 2013-03-23 00:00:00
3228 WWW US Dollar 2012-12-29 00:00:00
3229 WWW US Dollar 2013-06-15 00:00:00
3230 WWW US Dollar 2013-09-07 00:00:00
3231 WYLE NaN NaT
3232 YALUNI NaN NaT
3233 YKBNK NaN NaT
3234 YZCOAL NaN NaT
3235 ZACHRY NaN NaT
3236 ZAYOGR US Dollar 2013-03-31 00:00:00
3237 ZAYOGR US Dollar 2013-06-30 00:00:00
3238 ZAYOGR US Dollar 2012-12-31 00:00:00
3239 ZINC US Dollar 2013-06-30 00:00:00
3240 ZINC US Dollar 2012-12-31 00:00:00
3241 ZINC US Dollar 2013-03-31 00:00:00
In [58]: df.dtypes
Out[58]:
ticker object
currency object
date datetime64[ns]
dtype: object
In [59]: df.tail(7).groupby('ticker').last()
Out[59]:
currency date
ticker
ZACHRY NaN NaN
ZAYOGR US Dollar 2012-12-30T16:00:00.000000000-0800
ZINC US Dollar 2013-03-30T17:00:00.000000000-0700
In [60]: df.tail(6).groupby('ticker').last()
Out[60]:
currency date
ticker
ZAYOGR US Dollar 2012-12-31 00:00:00
ZINC US Dollar 2013-03-31 00:00:00
In [61]:
看起来只有当有 NaT 预设时,带有时间戳的列才会被弄乱。