0

I'm having a hard time getting Pandas resample to work. I don't want to set a datetime index but want to use a column with the dates. As I understood this should be possible. (When using a datetime index it does work)

this is my code:

import pandas as pd

data_in = [
    ["date","serial","event"],
    ["2021-08-11","6840","1"],
    ["2021-08-11","6840","1"],
    ["2021-08-12","6840","1"],
    ["2021-08-13","6840","1"],
    ["2021-08-15","6840","1"],
    ["2021-08-15","6840","1"],
    ["2021-08-16","6840","1"],
    ["2021-08-17","6840","1"],
    ["2021-08-17","6840","1"],
    ["2021-08-17","6840","1"],
    ["2021-08-18","6840","1"],
    ["2021-08-19","6840","1"],
    ["2021-08-21","6840","1"],
    ["2021-08-21","6840","1"],
    ["2021-09-01","6840","1"],
    ["2021-09-04","6840","1"],
    ["2021-09-04","6840","1"],
    ["2021-09-05","6840","1"],
    ["2021-09-07","6840","1"],
    ["2021-09-11","6840","1"],
]

col_names = data_in.pop(0)
work_data = pd.DataFrame(data_in,columns=col_names)
work_data["date"] = pd.to_datetime(work_data["date"])
work_data["event"] = pd.to_numeric(work_data["event"])

sum_data = work_data[["date","event"]]

sum_data = sum_data.resample("W",on=sum_data["date"]).sum()

Resample then trows an exception:

Exception has occurred: ValueError
The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

I'm not getting it. What is the error? What am I doing wrong? The documentation does not help me. The tutorials I saw where pretty straight forward and I'm not doing anything fancy.

Phil K.
  • 53
  • 5
  • 1
    It's just `sum_data = sum_data.resample("W", on="date").sum()` The `on` argument of [`DataFrame.resample`](https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.resample.html) is `str, optional` not `Series`. – Henry Ecker Sep 11 '21 at 17:17
  • 1
    Take a look at [this answer](https://stackoverflow.com/a/59125880/15497888) using the `on` argument of `resample`. – Henry Ecker Sep 11 '21 at 17:22
  • Thanks for the reply. This helped, thanks. I'm relatively new to Pandas and getting constantly confused by Pandas at one point requiring the `df["column"]` syntax and on on other point just the `"column"` syntax. – Phil K. Sep 11 '21 at 18:45

0 Answers0