gamzef
gamzef

Reputation: 37

Filtering the data in the dataframe according to the desired time in python

I am new to python and pandas dataframe. Here is my creation code of my dataframe.

data = pd.read_csv('trafikdensity.csv',parse_dates=True,index_col=0)

What you see below is first 5 rows of my dataframe. Result of the first method

I'd like to filter data between '07:00:00' and '21:00:00' for all days in January. This dataframe includes all days of January. First, I converted to datetime object and I tried to filter.

data['DATE_TIME'] = pd.to_datetime((data['DATE_TIME']))
data[data['DATE_TIME'].dt.strftime('%H:%M:%S').between('07:00:00','21:00:00')]

It gives me an empty data. Then, I tried to another method. It gives me also an empty data.

start_date01 = '2020-01-01 07:00:00'
end_date01 = '2020-01-01 21:00:00'
start_date02 = '2020-01-02 07:00:00'
end_date02 = '2020-01-02 21:00:00'
start_date03 = '2020-01-03 07:00:00'
end_date03 = '2020-01-03 21:00:00'

#greater than the start date and smaller than the end date
m1 = (data['DATE_TIME'] > start_date01) & (data['DATE_TIME'] <= end_date01) 
m2 = (data['DATE_TIME'] > start_date02) & (data['DATE_TIME'] <= end_date02) 
m3 = (data['DATE_TIME'] > start_date03) & (data['DATE_TIME'] <= end_date03)

data = data[np.logical_and.reduce([m1, m2, m3])]

Result of the second method

What am I missing? Or can I solve my problem with a different method? I took advantage of the solutions in Stackoverflow while doing these. But none of them have had a solution.

Upvotes: 1

Views: 58

Answers (1)

Kabilan Mohanraj
Kabilan Mohanraj

Reputation: 1906

data['DATE_TIME'] = pd.to_datetime(data['DATE_TIME'], yearfirst=True)

The yearfirst parameter has to be set to true.

Upvotes: 1

Related Questions