6

我在进行基本数据处理时遇到了这种行为,就像在这个例子中一样:

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() 从字符串中解析的时间戳系列的最小值/最大值

我究竟做错了什么?

4

1 回答 1

6

正如@meteore 所指出的,这是 NumPy 1.6.x 中 np.datetime64 类型的字符串 repr 的问题。基础数据,应该仍然是正确的。要解决此问题,您可以执行以下操作:

In [15]: df
Out[15]: 
   L                  TS         V
0  A 2000-01-01 00:00:00  0.752035
1  A 2000-01-01 04:00:00 -1.047444
2  A 2000-01-01 08:00:00  1.177557
3  B 2000-01-01 12:00:00  0.394590
4  B 2000-01-01 16:00:00  1.835067
5  B 2000-01-01 20:00:00 -0.768274
6  C 2000-01-02 00:00:00 -0.564037
7  C 2000-01-02 04:00:00 -2.644367
8  C 2000-01-02 08:00:00 -0.571187
9  C 2000-01-02 12:00:00  1.618557

In [16]: df.TS.astype(object).min()
Out[16]: datetime.datetime(2000, 1, 1, 0, 0)

In [17]: df.TS.astype(object).max()
Out[17]: datetime.datetime(2000, 1, 2, 12, 0)
于 2012-10-19T14:39:59.173 回答