熊猫:将日期从pandas.tslib.Timestamp转换为pandas.core.series.Series

时间:2020-05-09 09:12:00

标签: python pandas

我有两个不同类型的日期。我想比较两个日期。

qcurrent= pd.Series(query.index[-1])
qcurrent= pd.to_datetime(qcurrent, format='%Y-%m-%d').dt.date 

>>> qcurrent
0    2020-05-08
dtype: object
>>>type(current)
<class 'pandas.core.series.Series'>

qcsvdate = query1.index[-1][:10]datetime.strptime(query1.index[-1], "%Y-%m-%d")
>>> qcsvdate
datetime.datetime(2020, 5, 8, 0, 0)
>>> type(qcsvdate)
<class 'pandas.tslib.Timestamp'>

if qcurrent.iat[-1]>=qcsvdate[-1]:
    print " today is a Friday"
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'Timestamp' object does not support indexing

如何使两个日期都可以与条件相比较?

1 个答案:

答案 0 :(得分:0)

为qcsvdate添加pd.Series以标准化两种格式

qcsvdate= pd.Series(query1.index[-1])
qcsvdate = query1.index[-1][:10]datetime.strptime(query1.index[-1], "%Y-%m-%d")
>>>type(qcsvdate)
<class 'pandas.core.series.Series'>