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.date
.
Any advice?
I've tried pd.to_datetime().date
but this only works on a single item(e.g. df.ix[0,0]
), it won't let me apply to the entire series (e.g. df['mydates']
) or the dataframe.
Example 1: Python timestamp to datetimeThe fromtimestamp() function is used to return the date associated with a given timestamp. The date class's function fromtimestamp() computes and returns the date and time corresponding to a specified timestamp.
You can simply use the fromtimestamp function from the DateTime module to get a date from a UNIX timestamp. This function takes the timestamp as input and returns the corresponding DateTime object to timestamp.
Call dataframe[column] . dt. strftime(format) where dataframe[column] is the column from the DataFrame containing datetime objects and format is a string representing the new date format. Use "%m" to indicate where the month should be positioned, "%d" for the day, and "%y" for the year.
I got some help from a colleague.
This appears to solve the problem posted above
pd.to_datetime(df['mydates']).apply(lambda x: x.date())
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With