0

Im want to organice some columns by weeks but it throws this error: <' not supported between instances of 'int' and 'str' an example of the dataframe is:

id---- date ------ count

1 2019-04-26 4

2 2019-04-26 3

3 2019-04-26 2

the code i used is ep27=ep27.resample("W").sum() where ep27 is the dataframe

  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Apr 13 '22 at 07:35

1 Answers1

0

Welcome to StackOverflow! It can be helpful for us to see a minimal reproducible example that demonstrates your error. Otherwise we have to make additional, potentially incorrect, assumptions.

For what you have provided, it looks like Pandas is giving you a TypeError, indicating that Pandas received a incompatible data types for the comparison operation <. Trying to use a less than operation on a string and an integer can produce the same error:

>>> '2' < 2
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: '>' not supported between instances of 'str' and 'int'

The .dtypes method shows the data types for each column in your DataFrame. If your date column is listed as object this means it is a string and not a date. If so, you can see this answer on how to convert a string to a date. (When reading input from .csv files I often forget to apply the parse_date= keyword of pd.read_csv).

Here's a functional example of .resample(). The docs for resample do not state it clearly that I can tell, but one tricky aspect is that the date values must be in the index, which you can accomplish using .set_index('date').

import pandas as pd
import numpy as np

# create dataframe with on value per day
days = pd.date_range(start='2022-01-01', end='2022-01-31', freq='D')
counts = np.arange(days.shape[0])

df = pd.DataFrame(data={'date': days, 'count':counts})

# resample to sum over each week
df.set_index('date').resample('W').sum()
Alex K
  • 41
  • 4
  • You could use `on="date"` to `.resample` along column `date`. (And you don't need `np.arange()` to create the `counts` column, a simple `range()` is enough.) – Timus Apr 13 '22 at 12:45
  • While your expanation seems sensible, I can't reproduce the exact `TypeError` from the question with the given example. I get `TypeError: Only valid with DatetimeIndex, TimedeltaIndex or PeriodIndex, ...` - which makes sense? – Timus Apr 13 '22 at 12:59