2

Recently I started playing around with Python and have been stuck with an issue related to Pandas (particularly .loc) for a while now. Basically, I am just trying to manipulate the dataframe so that it only keeps certain date ranges.

When I filter for one specific date the command runs flawlessly but as soon as I switch for a range it won't. Also, any other variable type works fine - I'm surprised because people must have to filter for dates all the time. Do I have to transform the date?

# this works perfectly fine
raw_data.loc[raw_data['date_field'] == '2019-06-20', 'id':'date_field']

# this does not
raw_data.loc[raw_data['date_field'] >= '2019-06-20', 'id':'date_field']

I get a type error:

TypeError Traceback (most recent call last)

Now, I have tried all sorts of different transformations of the date field but keep getting the same error. Is it at all possible to filter dates using .loc?

LeroyFromBerlin
  • 395
  • 3
  • 12

1 Answers1

1

I think here is necessary convert column to datetimes by to_datetime:

raw_data['date_field'] = pd.to_datetime(raw_data['date_field'])
raw_data.loc[raw_data['date_field'] >= '2019-06-20', 'id':'date_field']
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252