所以你从 pandas 结构开始:
In [133]: x_month_begin
Out[133]:
DatetimeIndex(['2020-01-01', '2020-02-01', '2020-03-01', '2020-04-01',
'2020-05-01', '2020-06-01', '2020-07-01', '2020-08-01',
'2020-09-01', '2020-10-01', '2020-11-01', '2020-12-01'],
dtype='datetime64[ns]', freq='MS')
和 numpy 数组一样的东西:
In [134]: x_month_begin.values
Out[134]:
array(['2020-01-01T00:00:00.000000000', '2020-02-01T00:00:00.000000000',
'2020-03-01T00:00:00.000000000', '2020-04-01T00:00:00.000000000',
'2020-05-01T00:00:00.000000000', '2020-06-01T00:00:00.000000000',
'2020-07-01T00:00:00.000000000', '2020-08-01T00:00:00.000000000',
'2020-09-01T00:00:00.000000000', '2020-10-01T00:00:00.000000000',
'2020-11-01T00:00:00.000000000', '2020-12-01T00:00:00.000000000'],
dtype='datetime64[ns]')
你将它操纵成一个 (n,3) 数组(我怀疑这可以通过重塑和可能的转置更直接地完成):
In [135]: x_month_begin = np.vstack(np.split(x_month_begin, 3))
In [138]: x_month_begin = np.transpose(x_month_begin)
In [139]: x_month_begin
Out[139]:
array([['2020-01-01T00:00:00.000000000', '2020-05-01T00:00:00.000000000',
'2020-09-01T00:00:00.000000000'],
['2020-02-01T00:00:00.000000000', '2020-06-01T00:00:00.000000000',
'2020-10-01T00:00:00.000000000'],
['2020-03-01T00:00:00.000000000', '2020-07-01T00:00:00.000000000',
'2020-11-01T00:00:00.000000000'],
['2020-04-01T00:00:00.000000000', '2020-08-01T00:00:00.000000000',
'2020-12-01T00:00:00.000000000']], dtype='datetime64[ns]')
In [140]: _.shape
Out[140]: (4, 3)
任何方式,现在你的比较:
In [141]: st_d = pd.to_datetime('01/2016', format="%m/%Y")
In [142]: st_d
Out[142]: Timestamp('2016-01-01 00:00:00')
In [143]: x_month_begin >st_d
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-143-30567662e59d> in <module>
----> 1 x_month_begin >st_d
pandas/_libs/tslibs/c_timestamp.pyx in pandas._libs.tslibs.c_timestamp._Timestamp.__richcmp__()
TypeError: '<' not supported between instances of 'Timestamp' and 'int'
numpy 数组可以进行< 比较,但它们对于dtypes 的兼容有一定的规则。 (例如,比较字符串和数字不起作用)。另外pandas玩自己的日期和时间游戏,有些格式是内部的,有些兼容numpy的datatime64。
例如,如果我们将您的时间戳转换为等效的 numpy:
In [144]: st_d.to_numpy()
Out[144]: numpy.datetime64('2016-01-01T00:00:00.000000000')
比较有效:
In [145]: x_month_begin>st_d.to_numpy()
Out[145]:
array([[ True, True, True],
[ True, True, True],
[ True, True, True],
[ True, True, True]])
pandas 建立在numpy 之上,或者至少使用numpy 数组来存储其数据。但是没有一个numpy 代码是pandas 知道的。如果给定一个非 numpy 对象,它会天真地尝试转换它,例如
In [146]: np.asarray(st_d)
Out[146]: array(Timestamp('2016-01-01 00:00:00'), dtype=object)
不同于Out[144]。 [146] 是产生错误的转换。
The original `DatetimeIndex` can be tested against the timestamp. That's a 'pure' pandas operation.
In [152]: _133>st_d
Out[152]:
array([ True, True, True, True, True, True, True, True, True,
True, True, True])
_133.to_numpy().reshape(-1,4).T 直接给出x_month_begin 数组。