Home > front end >  Why can't I select whole days from intra-day time series?
Why can't I select whole days from intra-day time series?

Time:11-18

I have basic financial OHLCV data.

                      Datetime        Open    Volume        date
0    2021-08-25 09:30:00-04:00  149.699997   3524920  2021-08-25
1    2021-08-25 09:35:00-04:00  149.699997   1424746  2021-08-25
2    2021-08-25 09:40:00-04:00  149.785004   1248013  2021-08-25
3    2021-08-25 09:45:00-04:00  149.649994   1208242  2021-08-25
4    2021-08-25 09:50:00-04:00  149.486603   1345607  2021-08-25

Let's say I want to select all the examples from one specific day.

df["Datetime"].eq("2021-08-25")

This doesn't work even though comparisons to string date work when you use lt or gt.

So I created the date column with

df["date"] = df["Datetime"].dt.date

This new column is than of object dtype yet:

df_n["date"].eq("2021-08-25")

Still doesn't work.

CodePudding user response:

One solution is 'remove' times, it means set them to 00:00:00 by Series.str.normalize, because obviously pandas compare strings datetimes with timestamps:

df["Datetime"].dt.normalize().eq("2021-08-25")
df["Datetime"].dt.floor('d').eq("2021-08-25")

If need compare dates, need dates in another side too:

from datetime import date

df["Datetime"].dt.date.eq(date(2021,8,25))

CodePudding user response:

Using strftime can also do

df1['Datetime']= pd.to_datetime(df1['Datetime'] )
df1[df1['Datetime'].dt.strftime('%Y-%m-%d')=='2021-08-25']
  • Related