0

I have a dataframe that update every week and I want to drop the data older than 6 months

For example:

I have a dataframe from January until now.

Now it's September 14 and I want to drop the old data, in this case from January until March 14.

In the case we are in December, it´ll have from June until December, and so on.

Thank you

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
  • 1
    Does this answer your question? [Deleting DataFrame row in Pandas based on column value](https://stackoverflow.com/questions/18172851/deleting-dataframe-row-in-pandas-based-on-column-value) – Trenton McKinney Sep 15 '20 at 00:28

1 Answers1

2
  • Months are an arbitrary time period, since the length changes
  • Use Boolean Indexing and filter against the current date minus 182 days
  • Alternative, use relativedelta from the python dateutil module, which can do months
from datetime import datetime
import pandas as pd
from dateutil.relativedelta import relativedelta as rd

# This line is just for creating test data
df = pd.DataFrame({'datetime': pd.date_range(start='2020-01-01', end=datetime.today(), freq='1d').to_pydatetime().tolist()})

# filter out the everything greater than 182 days
df_updated = df[df.datetime > datetime.today() - pd.Timedelta(days=182)]

# alternatively, use the relativedelta
df_updated = df[df.datetime > datetime.today() - rd(months=6)]
Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158