Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

only pull rows for today's date from dataframe

I'm pulling data from an API and placing it into a Pandas dataframe. I want to then create a new df that includes only the rows that have today's date in. I know how to select between two static dates, but can't seem to filter by a 'today' timestamp.

from matplotlib import pyplot as plt

#Access API
r = requests.get('REMOVED')
x = r.json()
keys = x.keys()
old_df = pd.DataFrame(x['results'])

#set dataframe
df = old_df[['valid_from','valid_to','value_inc_vat']].copy()

df['valid_from'] = pd.to_datetime(df['valid_from'])
df['valid_to'] = pd.to_datetime(df['valid_to'])

#only today's rows
today = pd.Timestamp.today().date()
mask = (df['from'] == today)
df_today = df.loc[mask]```
like image 513
ElCrouchoGrande Avatar asked May 02 '26 05:05

ElCrouchoGrande


1 Answers

Use Series.dt.date for compare by dates:

mask = (df['from'].dt.date == today)
df_today = df[mask]
like image 187
jezrael Avatar answered May 03 '26 20:05

jezrael



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!