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

What's the nicest most-pythonic way to select rows in pandas for datetime conditions

Filtering rows for Scalar and Logical conditions are quite well-known and reported here, e.g. df[df['col']>=0] can be used to filter the negative rows out. Yet, I’ve come to work with datetime and wonder what ways work, I might answer with approaches I find or try out myself.

>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

In pandas, as long as you have all of your datetime data formatted as datetime, it behaves very similiarly. Here are some examples and an additional link to reference.

Filter row row between two dates.

df[(df['date'] > '2019-12-01') & (df['date'] < '2019-12-31')]

Filter rows by index

df2.loc['2019-12-01':'2019-12-31']

https://datascientyst.com/filter-by-date-pandas-dataframe/#:~:text=Here%20are%20several%20approaches%20to%20filter%20rows%20in,3%29%20Filter%20rows%20by%20date%20with%20Pandas%20query

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