2

I have a dataframe, "df", which has a datetime index and another index column called "Location":

                        V1   V2  
Date       Location             
2001-01-01  1           0.5  0.7
            2           0.6  0.5
2001-01-02  3           0.8  0.2
            4           0.8  0.2
2001-01-03  5           0.2  0.4
            6           0.2  0.5
2001-01-04  7           0.2  0.3
            8           0.8  0.7

As you can see, the dataframe has multiple observations under the same date.

To be able to use some statistical packages, I have to set the frequency of the dataframe to "days" using this method:

df = df.asfreq('d')

However, the dataframe has two index columns; one datetime and another which is not. When I tried to set frequency using the approach which is in the captioned codeblock, I got this error:

TypeError: Cannot convert input [(Timestamp('2002-07-23 00:00:00+0000', tz='UTC'), '1')] of type to Timestamp

If I try to set just the date as the index column, I end up with the case of the same date appearing multiple times in the frame. In short; Pandas interprets these repeated instances as duplicates.

How would you resolve this issue?

1 Answer 1

2

There is MultiIndex, so possible solution is reshape by DataFrame.unstack first for DatetimeIndex and then reshape back by DataFrame.stack:

df = df.unstack().asfreq('d').stack()
print (df)
                      V1   V2
Date       Location          
2001-01-01 1         0.5  0.7
           2         0.6  0.5
2001-01-02 3         0.8  0.2
           4         0.8  0.2
2001-01-03 5         0.2  0.4
           6         0.2  0.5
2001-01-04 7         0.2  0.3
           8         0.8  0.7
1
  • 1
    Unfortunately, there doesn't seem to be a way to "set" the frequency of a series with a multi index. A multi-index series just can't have a frequency - maybe to avoid dealing with multiple datetime columns in the index, etc
    – iggy
    Commented Feb 12, 2021 at 18:28

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