How do I get the average month and day from a list of date times?

RockiesSkier

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".

ddejohn

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.

edited at
0

Comments

0 comments
Login to comment

Related

How do i get the date from week of month and day of week with Carbon?

How do I calculate the date in JavaScript, to get the same day in month + some month (variable)

How do I get the day of the month in javascript?

How do I get the last day of a month?

Get Month & Day from Date

How do i get first date and Last date from month and year |Excel |Used Spinner for Month and Year|

Java: How do I get the date of x day in a month ( e.g. Third Monday in February 2012)

How do I filter a list with date and time values by only extracting specific times each day?

How do I specify from a date to the last occurence of a day in a specific month, using iCal?

How can I get the last day of the previous month from a date entered?

How to get the server date (i.e month and day) in magento?

how to get year month day from long date?

How to get day,month,year from String date?

MomentJS - How to get last day of previous month from date?

How to get year/month/day from a date object?

How to get Day and Month from Date Sql Query?

How to get only day and month of a date from custom field of wordpress?

how to get the last day of the month from a given date

How to get a Date object from year,month and day?

How to get the day number of a date from a month in momentjs?

How do I get the average row count per day of data?

How do I extract the Hour/Minute from a datetime object and get rid of the Year/Month/Day/Second section?

How do I get the day month and year columns?

php get the upcoming date from month of day

Get next date from day-month

How to get a day of month from "what I know"?

How do we get the date to the last Friday of the month when the last day of the month falls on a weekend

How to get any day's date in a month?

How to get the date from year, month, week of the month, and day of the week in php