Pandas: Convert Timestamp to datetime.date

后端 未结 4 1356
名媛妹妹
名媛妹妹 2020-12-13 06:00

I have a pandas column of Timestamp data

In [27]: train[\"Original_Quote_Date\"][6] 
Out[27]: Timestamp(\'2013-12-25 00:00:00\')

How can ch

4条回答
  •  醉梦人生
    2020-12-13 06:31

    Use the .date method:

    In [11]: t = pd.Timestamp('2013-12-25 00:00:00')
    
    In [12]: t.date()
    Out[12]: datetime.date(2013, 12, 25)
    
    In [13]: t.date() == datetime.date(2013, 12, 25)
    Out[13]: True
    

    To compare against a DatetimeIndex (i.e. an array of Timestamps), you'll want to do it the other way around:

    In [21]: pd.Timestamp(datetime.date(2013, 12, 25))
    Out[21]: Timestamp('2013-12-25 00:00:00')
    
    In [22]: ts = pd.DatetimeIndex([t])
    
    In [23]: ts == pd.Timestamp(datetime.date(2013, 12, 25))
    Out[23]: array([ True], dtype=bool)
    

提交回复
热议问题