Home > Enterprise >  Resample datetime - Error: cannot reindex a non-unique index with a method or limit
Resample datetime - Error: cannot reindex a non-unique index with a method or limit

Time:06-09

Would you help me with the following error:

ValueError: cannot reindex a non-unique index with a method or limit

Let's say I have a dataframe df

datetime                   A      B      C
2020-07-02 23:00:01    50     nan    nan
2020-07-02 23:00:02    nan    60     nan
2020-07-02 23:00:11    nan    nan    80
2020-07-02 23:14:01    nan    nan    65
2020-07-02 23:15:01    nan    90     nan
2020-07-02 23:15:02    10     nan    nan
2020-07-02 23:28:01    20     25     nan
2020-07-02 23:30:01    nan    nan    80

What I am trying to achieve is the following output:

datetime                   A      B      C
2020-07-02 23:00:00    50     60    80
2020-07-02 23:15:00    10    90    65
2020-07-02 23:30:01    20    25    80

I tried this with the following code:

df.datetime = pd.to_datetime(dfinal.datetime)
resampledata = df.set_index("tijd").resample("15T").pad()

But I got an error probably due to not having unique datetime values.

CodePudding user response:

Assume your dataframe called df, use pd.Grouper

df['datetime'] = pd.to_datetime(df['datetime'])
df.set_index('datetime').groupby(pd.Grouper(freq='15min')).sum()

The output will be:

| datetime            |   A |   B |   C |
|:--------------------|----:|----:|----:|
| 2020-07-02 23:00:00 |  50 |  60 | 145 |
| 2020-07-02 23:15:00 |  30 | 115 |   0 |
| 2020-07-02 23:30:00 |   0 |   0 |  80 |

Of course, you can change the range as you wish

  • Related