Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Use Series.dt.date for compare by dates:

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

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading