How do I convert timestamp to datetime.date in pandas dataframe?

前端 未结 7 1886
清酒与你
清酒与你 2020-12-03 14:03

I need to merge 2 pandas dataframes together on dates, but they currently have different date types. 1 is timestamp (imported from excel) and the other is datetime.dat

7条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-03 14:40

    I found the following to be the most effective, when I ran into a similar issue. For instance, with the dataframe df with a series of timestmaps in column ts.

    df.ts.apply(lambda x: pd.datetime.fromtimestamp(x).date())
    

    This makes the conversion, you can leave out the .date() suffix for datetimes. Then to alter the column on the dataframe. Like so...

    df.loc[:, 'ts'] = df.ts.apply(lambda x: pd.datetime.fromtimestamp(x).date())
    

提交回复
热议问题