Katelin Avenir
Katelin Avenir

Reputation: 1

Pulling a certain date from datetime column python

How do you pull a certain date from a datetime column. I have been using this

df.loc[(df['column name'] == 'date')]

But it cannot find the date although it is in the df.

Upvotes: 0

Views: 712

Answers (1)

Raspberry PyCharm
Raspberry PyCharm

Reputation: 104

Your datetime column has probably smaller granularity than just the date(year,month,day), the default in pandas is nanoseconds(ns) but it could also be just seconds in your case, depending on the data source. You can see the dtype by accessing df.column.dtype yourself, and it also helps with the case when you column isnt actually of datetime dtype, in which case you need to cast it to datetime first.

And '2001-15-12' is not equal to '2001-15-12 18:36:45:2242' Neither '2001-15-12' to '2001-15-12 18:36:45'

If you only need dates, set the datetime colum to just the date like this, using the .dt accesor for datetime segments:

df['column name'] = df['column name'].dt.date

Then you'll be able to access

df.loc[(df['column name'] == 'date')] #using just the year, month and date in the format above.

Upvotes: 1

Related Questions