Home > other >  How to generate a series containing each date for the following month relative to a given date in pa
How to generate a series containing each date for the following month relative to a given date in pa

Time:03-21

My start point is the variable data_published_date

data_published_date
Out[47]: "DatetimeIndex(['2022-03-18'], dtype='datetime64[ns]', freq=None)"

as it's a date in March, I wish to generate EACH day for the NEXT month as timestamp, like

['2022-04-01', '2022-04-02', '2022-04-03'.....'2022-04-30']

I tried

index = pd.date_range(data_published_date   pd.offsets.MonthBegin(n=1), data_published_date   pd.offsets.MonthEnd(n=1))

and received

TypeError: Cannot convert input [DatetimeIndex(['2022-04-01'], dtype='datetime64[ns]', freq=None)] of type <class 'pandas.core.indexes.datetimes.DatetimeIndex'> to Timestamp

tried then 2 steps

start_date = data_published_date   pd.offsets.MonthBegin(n=1)
end_date = data_published_date   pd.offsets.MonthEnd(n=2)

but can't find a solution to convert these 2 Datetimeindex to timestamp so that I can use pd.date_range to reach my objective.

Any idea?

CodePudding user response:

I think the problem is data_published_date is Index object, but date_range is expecting a singleton. Since it contains only a single element, we could index it and use that instead:

out = pd.date_range(data_published_date[0]   pd.offsets.MonthBegin(n=1), 
                    data_published_date[0]   pd.offsets.MonthEnd(n=2))

Output:

DatetimeIndex(['2022-04-01', '2022-04-02', '2022-04-03', '2022-04-04',
               '2022-04-05', '2022-04-06', '2022-04-07', '2022-04-08',
               '2022-04-09', '2022-04-10', '2022-04-11', '2022-04-12',
               '2022-04-13', '2022-04-14', '2022-04-15', '2022-04-16',
               '2022-04-17', '2022-04-18', '2022-04-19', '2022-04-20',
               '2022-04-21', '2022-04-22', '2022-04-23', '2022-04-24',
               '2022-04-25', '2022-04-26', '2022-04-27', '2022-04-28',
               '2022-04-29', '2022-04-30'],
              dtype='datetime64[ns]', freq='D')

CodePudding user response:

Here is a working solution


pd.date_range(start=(data_published_date   pd.offsets.MonthBegin(n=1)).date[0], 
end=(data_published_date   pd.offsets.MonthBegin(n=2)).date[0]-pd.Timedelta(days=1))

Output:


DatetimeIndex(['2022-04-01', '2022-04-02', '2022-04-03', '2022-04-04',
               '2022-04-05', '2022-04-06', '2022-04-07', '2022-04-08',
               '2022-04-09', '2022-04-10', '2022-04-11', '2022-04-12',
               '2022-04-13', '2022-04-14', '2022-04-15', '2022-04-16',
               '2022-04-17', '2022-04-18', '2022-04-19', '2022-04-20',
               '2022-04-21', '2022-04-22', '2022-04-23', '2022-04-24',
               '2022-04-25', '2022-04-26', '2022-04-27', '2022-04-28',
               '2022-04-29', '2022-04-30'],
              dtype='datetime64[ns]', freq='D')

I got the start of the next two months and subtracted 1 in the later month to have a range of one month alone.

  • Related