ElCrouchoGrande
ElCrouchoGrande

Reputation: 57

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]```

Upvotes: 2

Views: 1547

Answers (1)

jezrael
jezrael

Reputation: 862701

Use Series.dt.date for compare by dates:

mask = (df['from'].dt.date == today)
df_today = df[mask]

Upvotes: 3

Related Questions