I have the following list of date times:
date_list = [Timestamp('2015-05-17 00:00:00'), Timestamp('2016-04-28 00:00:00'), Timestamp('2017-05-17 00:00:00'), Timestamp('2018-05-09 00:00:00'), Timestamp('2019-06-04 00:00:00'), Timestamp('2020-04-28 00:00:00')]
How do I get the average day and month from this list? My current code is as follows:
avg_month_day = datetime.strftime(datetime.fromtimestamp(sum(map(datetime.timestamp,date_list))/len(date_list)),"%m-%d")
However, with the inclusion of the year the result of the above code is "11-10" while I believe it should be "05-12".
You're getting that average because the year is being taken into account. You need to construct new timestamps with an identical year in order to get the result you want.
There may be a more elegant solution but here's one that should work just fine:
import pandas as pd
pd.Series(date_list).apply(lambda d: pd.Timestamp(2020, d.month, d.day)).mean()
Output:
Timestamp('2020-05-12 08:00:00')
From there, you can create your datetime object formatted however you want.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments