So I thought this should have been a simpler task but I could not find it. I have a pandas dataframe as follows
Company Id DateTime col1 col2 col3 col4 col5 col6
0 25502921 2018-08-16 10:23:36 0 175.000 0.0 0.0 0.0 0
1 25502921 2018-08-16 10:33:55 0 155.557 0.0 0.0 0.0 0
2 25502921 2018-08-16 10:43:55 0 153.615 0.0 0.0 0.0 0
type(df['DateTime'][0])
outputs pandas._libs.tslibs.timestamps.Timestamp
How can i subset the dataframe based on a date?
df_tmp = df[df['DateTime'].dt.date=='16-08-2018']
does not seem to work nor does df_tmp = df[df['DateTime']=='16-08-2018']
What are the other intuitive methods i can try?
use .dt.normalize()
which converts the time to 00:00:00
but returns a datetime object.
df['DateTime'].dt.normalize().apply(type)
pandas._libs.tslibs.timestamps.Timestamp
print(df['DateTime'].dt.normalize())
0 2018-08-16
1 2018-08-16
2 2018-08-16
Name: DateTime, dtype: datetime64[ns]
df[df['DateTime'].dt.normalize() == '2018-08-16']
Company_Id DateTime col1 col2 col3 col4 col5 col6
0 25502921 2018-08-16 10:23:36 0 175.000 0.0 0.0 0.0 0
1 25502921 2018-08-16 10:33:55 0 155.557 0.0 0.0 0.0 0
2 25502921 2018-08-16 10:43:55 0 153.615 0.0 0.0 0.0 0
I believe u'll get better performance and easier processing when u set the DateTime as the index:
df = pd.read_clipboard(sep = '\s{2,}',parse_dates=['DateTime'])
df.set_index("DateTime").loc["2018-08-16"]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.