我在进行基本数据处理时遇到了这种行为,就像在这个例子中一样:
In [55]: import pandas as pd
In [56]: import numpy as np
In [57]: rng = pd.date_range('1/1/2000', periods=10, freq='4h')
In [58]: lvls = ['A','A','A','B','B','B','C','C','C','C']
In [59]: df = pd.DataFrame({'TS': rng, 'V' : np.random.randn(len(rng)), 'L' : lvls})
In [60]: df
Out[60]:
L TS V
0 A 2000-01-01 00:00:00 -1.152371
1 A 2000-01-01 04:00:00 -2.035737
2 A 2000-01-01 08:00:00 -0.493008
3 B 2000-01-01 12:00:00 -0.279055
4 B 2000-01-01 16:00:00 -0.132386
5 B 2000-01-01 20:00:00 0.584091
6 C 2000-01-02 00:00:00 -0.297270
7 C 2000-01-02 04:00:00 -0.949525
8 C 2000-01-02 08:00:00 0.517305
9 C 2000-01-02 12:00:00 -1.142195
问题:
In [61]: df['TS'].min()
Out[61]: 31969-04-01 00:00:00
In [62]: df['TS'].max()
Out[62]: 31973-05-10 00:00:00
虽然这看起来不错:
In [63]: df['V'].max()
Out[63]: 0.58409076701429163
In [64]: min(df['TS'])
Out[64]: <Timestamp: 2000-01-01 00:00:00>
在 groupby 之后聚合时:
In [65]: df.groupby('L').min()
Out[65]:
TS V
L
A 9.466848e+17 -2.035737
B 9.467280e+17 -0.279055
C 9.467712e+17 -1.142195
In [81]: val = df.groupby('L').agg('min')['TS']['A']
In [82]: type(val)
Out[82]: numpy.float64
显然,在这种特殊情况下,它与使用频率日期时间索引作为 pd.Series 函数的参数有关:
In [76]: rng.min()
Out[76]: <Timestamp: 2000-01-01 00:00:00>
In [77]: ts = pd.Series(rng)
In [78]: ts.min()
Out[78]: 31969-04-01 00:00:00
In [79]: type(ts.min())
Out[79]: numpy.datetime64
但是,我最初的问题是通过 pd.read_csv() 从字符串中解析的时间戳系列的最小值/最大值
我究竟做错了什么?