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

I need to combine 2 pandas dataframes together by dates, but they currently have different types of dates. 1 - timestamp (imported from excel), and the other - datetime.date.

Any tips?

I tried pd.to_datetime().date, but it only works on one element (e.g. df.ix[0,0]), it will not allow me to apply to the entire series (e.g., df['mydates']) or to the data frame.

+6
source share
4 answers

I got some help from a colleague.

This seems to solve the problem above

pd.to_datetime(df['mydates']).apply(lambda x: x.date())

+10
source

datetime.date... .date Timestamp

pd.to_datetime(df['mydates']).date
0

, , , (, , / ):

pandas pandas.Timestamp.to_pydatetime " Timestamp datetime Python".

0

, . , df ts.

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

, .date() .date() datetime. . ...

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

Source: https://habr.com/ru/post/1667445/


All Articles