Python 将2级grupby对象与min()聚合后,出现意外的(原始)时间戳数据类型
这可能与已解决的问题/错误有关: 这一次,问题在于在多级groupby对象中的Timestamp/datetime列上使用min()和max()方法进行聚合。结果列似乎是整数,而不是预期的Timestamp/datetimePython 将2级grupby对象与min()聚合后,出现意外的(原始)时间戳数据类型,python,group-by,pandas,timestamp,Python,Group By,Pandas,Timestamp,这可能与已解决的问题/错误有关: 这一次,问题在于在多级groupby对象中的Timestamp/datetime列上使用min()和max()方法进行聚合。结果列似乎是整数,而不是预期的Timestamp/datetime In [3]: pd.__version__ Out[3]: '0.13.0' In [4]: np.__version__ Out[4]: '1.8.0' In [5]: rng = pd.date_range('1/1/2000', periods=10, fre
In [3]: pd.__version__
Out[3]: '0.13.0'
In [4]: np.__version__
Out[4]: '1.8.0'
In [5]: rng = pd.date_range('1/1/2000', periods=10, freq='4h')
In [6]: lvl1 = ['A','A','A','A','A','B','B','B','B','B']
In [7]: lvl2 = ['foo','foo','bar','bar','bar','foo','foo','foo','bar','bar']
In [8]: df = pd.DataFrame({'TS': rng, 'V' : np.random.randn(len(rng)), 'L1' : lvl1, 'L2' : lvl2})
In [9]: df
Out[9]:
L1 L2 TS V
0 A foo 2000-01-01 00:00:00 1.084908
1 A foo 2000-01-01 04:00:00 1.650871
2 A bar 2000-01-01 08:00:00 1.884018
3 A bar 2000-01-01 12:00:00 -1.196144
4 A bar 2000-01-01 16:00:00 -1.249594
5 B foo 2000-01-01 20:00:00 -1.426138
6 B foo 2000-01-02 00:00:00 0.413785
7 B foo 2000-01-02 04:00:00 0.507160
8 B bar 2000-01-02 08:00:00 1.369455
9 B bar 2000-01-02 12:00:00 2.229319
在一个级别的groupby上使用min()似乎可以像预期的那样工作,时间戳就是时间戳
In [10]: df.groupby(['L1']).min()
Out[10]:
L2 TS V
L1
A bar 2000-01-01 00:00:00 -0.995938
B bar 2000-01-01 20:00:00 -1.582318
这是令人不安的部分;使用min()聚合2级grouby对象后,TS列位于int64中
In [11]: df.groupby(['L1', 'L2']).min()
Out[11]:
TS V
L1 L2
A bar 946713600000000000 -0.607672
foo 946684800000000000 -0.995938
B bar 946800000000000000 -0.019575
foo 946756800000000000 -1.582318
In [12]: df.groupby(['L1', 'L2']).min().dtypes
Out[12]:
TS int64
V float64
dtype: object
从0.13.1开始,问题似乎已经解决。哦,我想这个问题谢谢@unutbu