3

this is how my dataset looks like

Datetime  MinDistance AvgDiameter RelativeV InfinityV               
1900-01-04  0.00962 410.0   8.69    8.65
1900-01-11  0.03989 59.5    10.65   10.65
1900-01-29  0.02076 880.0   5.55    5.52
1900-02-04  0.03201 65.0    3.13    3.11
1900-02-05  0.04903 151.0   10.97   10.97

this is how I imported the data

df = pd.read_csv("ddataset.csv", parse_dates=['Date'])
df['Datetime'] = pd.to_datetime(df['Date'])
df = df.set_index('Datetime')
df.drop(['Date'], axis=1, inplace=True)
print(df.shape)
df.head()

I'm trying to fit a VAR model for this data. I want to make datetime as my index column. I tried this,

df.index = pd.DatetimeIndex(df.index).to_period('M')
PeriodIndex(['1900-01', '1900-01', '1900-01', '1900-02', '1900-02', '1900-02',
             '1900-02', '1900-02', '1900-03', '1900-03',
             ...
             '2020-04', '2020-04', '2020-04', '2020-04', '2020-04', '2020-04',
             '2020-04', '2020-04', '2020-04', '2020-04'],
            dtype='period[M]', name='Datetime', length=9908, freq='M'

It works, but I want to set my index frequency as 'Daily'. I replaced 'M' with 'd' and 'D' and It does not work. What should I do? Is there any other way to change the frequency?

1
  • 2
    The period of the underlying data is probably lower than daily. You will need to resample by calling df.resample('D').asfreq(). There will be some NaN values introduced for the missing dates which have to be handled with a method like fillna()
    – IKBreier
    Commented Aug 19, 2020 at 15:04

1 Answer 1

1

This works for me and the problem was that you have periodIndex instead of DatetimeIndex object.

df.set_index('Datetime',inplace=True)
df.index=df.index.to_period('D')

In fact, 'PeriodIndex' object has no attribute 'to_period'

Not the answer you're looking for? Browse other questions tagged or ask your own question.