anoldmaninthesea
In pandas, I can create a TimedeltaIndex array using for example:
```
pd.timedelta_range(0,periods=9,freq="2H30T")
```
In the code above, `freq="2H30T"` gives us the period's interval of 2 hours and 30 minutes.
Its output will be the following:
```
TimedeltaIndex(['00:00:00', '02:30:00', '05:00:00', '07:30:00', '10:00:00',
'12:30:00', '15:00:00', '17:30:00', '20:00:00'],
dtype='timedelta64[ns]', freq='150T')
```
So, I tried to play with the [pandas time strings list](https://pandas.pydata.org/pandas-docs/stable/user_guide/timeseries.html#offset-aliases).
I wanted to define the period's interval as 2 years and 5 months, and using the time string list above, I came up with
```
pd.timedelta_range(0,periods=9,freq="2A5M")
```
However, this doesn't work, and outpours the following error message:
`ValueError: Invalid frequency: 2A5M`.
I've also tried 2Y5M, and similarly got an error message.
Top Answer
Anonymous 1581
A partial answer:
```python
import pandas as pd
import datetime
start = datetime.datetime(2011, 1, 1)
```
With that start anchor, `pd.date_range` allows for specifying dates that are "2 years and 5 months" apart (either anchored at the end of the month `M` or at the start `MS`):
```
In [18]: pd.date_range(start, periods=9,freq="29M")
Out[18]:
DatetimeIndex(['2011-01-31', '2013-06-30', '2015-11-30', '2018-04-30',
'2020-09-30', '2023-02-28', '2025-07-31', '2027-12-31',
'2030-05-31'],
dtype='datetime64[ns]', freq='29M')
In [17]: pd.date_range(start, periods=9,freq="29MS")
Out[17]:
DatetimeIndex(['2011-01-01', '2013-06-01', '2015-11-01', '2018-04-01',
'2020-09-01', '2023-02-01', '2025-07-01', '2027-12-01',
'2030-05-01'],
dtype='datetime64[ns]', freq='29MS')
```
The `timedelta_range` cannot work, because those calendaric periods can only be instantiated with a known reference date to calculate specific month durations.