As title I have a data named user like
reporttime
0 2020-01-01 00:00:17
1 2020-01-01 00:03:17
2 2020-01-01 00:06:17
3 2020-01-01 00:09:17
. .
. .
. .
2020-12-30 23:59:04
and I want to delete all data about 2020-01-02
So I try
user.drop(user[(user['reporttime'].dt.month.isin(np.arange(0,1)))(user['reporttime'].dt.day.isin(np.arange(2,3)))],axis = 1,inplace= True)
But it failed and it drop all data where am i doing wrong?
CodePudding user response:
You might use .dt.date to access date, consider following example:
import pandas as pd
import datetime
df = pd.DataFrame({"reporttime":pd.to_datetime(["2020-01-01 00:00:17","2020-01-01 00:09:17","2020-12-30 23:59:04"])})
df2 = df[df['reporttime'].dt.date != datetime.date(2020,1,1)]
print(df2)
output
reporttime
2 2020-12-30 23:59:04
Note: I limited initial df to 3 records for brevity sake.
CodePudding user response:
I didn't try to debug your code but, if the dates are stored as strings and not timeseries. You can use a simple mask to achieve this.
dateMask = (user['reporttime'] > '2020-01-02 00:00:00') &
(user['reporttime'] < '2020-01-02 23:59:59')
user = user[~dateMask] #this also gets rid of any other columns related to that date
