How Python Gets the Appearance Frequency of a Month from the Date List

Asked 2 years ago, Updated 2 years ago, 224 views

The following date data are available:

DatetimeIndex('2019-06-05', '2019-06-14', '2019-06-24', '2019-07-03',
               '2019-07-30', '2019-07-31', '2019-08-06', '2019-08-28',
               '2019-09-03', '2019-09-26', '2019-10-11', '2019-10-18',
               '2019-10-24', '2019-11-19', '2019-11-20', '2019-12-17',
               '2019-12-19', '2019-12-20', '2020-01-15', '2020-01-28',
               '2020-02-17', '2020-02-21', '2020-03-03', '2020-03-17',
               '2020-03-31', '2020-04-06', '2020-04-24', '2020-05-11',
               '2020-06-18', '2020-06-26', '2020-07-27', '2020-08-28',
               '2020-08-31'],
              dtype='datetime64[ns]', freq=None)

How do we get the frequency of a month's appearance from here?
For example, it will be twice in April 2020 and three times in July 2019.
If you enter the desired year and year, you can imagine a function that returns the number of appearances, but there is no policy.

Thank you for your cooperation.

★Addition★
I'm sorry, but could you tell me one more thing?
How do you get the frequency of appearance over the past 30 days?
For example, if Today='2020-08-31', it will be twice.
If you enter a date, you can count from that date and return the number of appearances for the past 30 days.
Please.

python pandas

2022-09-30 21:53

2 Answers

How do you feel like this?

days=pd.to_datetime('2019-06-05','2019-06-14','2019-06-24','2019-07-03',
               '2019-07-30', '2019-07-31', '2019-08-06', '2019-08-28',
               '2019-09-03', '2019-09-26', '2019-10-11', '2019-10-18',
               '2019-10-24', '2019-11-19', '2019-11-20', '2019-12-17',
               '2019-12-19', '2019-12-20', '2020-01-15', '2020-01-28',
               '2020-02-17', '2020-02-21', '2020-03-03', '2020-03-17',
               '2020-03-31', '2020-04-06', '2020-04-24', '2020-05-11',
               '2020-06-18', '2020-06-26', '2020-07-27', '2020-08-28',
               '2020-08-31'])

def getDays (month):
    return pd.Series(index=days).to_period("M").index.value_counts().get(month, 0)

print(getDays('2020-04')
print(getDays('2019-07')


2022-09-30 21:53

Here's how to use pandas.DataFrame.resample.

import pandas as pd

default_frequency_by_month(date_series, month):
  sampling=date_series.to_frame().resample('M').size()
  return sampling [month][0] if month in sampling else0

if__name__=='__main__':
  dates=pd.DatetimeIndex(
    ['2019-06-05', '2019-06-14', '2019-06-24', '2019-07-03',
     '2019-07-30', '2019-07-31', '2019-08-06', '2019-08-28',
     '2019-09-03', '2019-09-26', '2019-10-11', '2019-10-18',
     '2019-10-24', '2019-11-19', '2019-11-20', '2019-12-17',
     '2019-12-19', '2019-12-20', '2020-01-15', '2020-01-28',
     '2020-02-17', '2020-02-21', '2020-03-03', '2020-03-17',
     '2020-03-31', '2020-04-06', '2020-04-24', '2020-05-11',
     '2020-06-18', '2020-06-26', '2020-07-27', '2020-08-28',
     '2020-08-31'', dtype='datetime64[ns]', freq=None)

  print(get_frequency_by_month(dates, '2020-04'))
  print(get_frequency_by_month(dates, '2019-07'))
  print(get_frequency_by_month(dates, '2020-10')))

=>
2
3
0


2022-09-30 21:53

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.