송준석
송준석

Reputation: 1031

how can i find values using timestamp in dataframe python

i try to find a value using timestamp like under data.

    date               foo  bar
2019-01-26 22:22:12     1   100 
2018-11-07 13:12:26     1   100 
2019-01-20 06:30:39     1   100 
2018-11-05 21:21:29     0   80  
2018-11-06 09:01:39     0   100 

when i use under code, Series not supported it.

tmp = df[df.date.day == 20]

so i using loop that every rows readed and pharsing data.

for i in range(len(df)):
    if df.date[i].day == 20:
               .
               .
               .

but i think it too long to finish my all data.

Is there a way to find a value using a timestamp?

Upvotes: 0

Views: 187

Answers (1)

mtndoe
mtndoe

Reputation: 444

Is it possible to clarify your question further? If I understand correct you are loading your data into a pandas dataframe. After loading the data you want to filter it based on the day value. If this is the case one option would be following.

import pandas as pd

# Create basic dataframe

df = pd.DataFrame([{'date' : '2019-01-26 22:22:12','foo': 1,'bar':   100 },
{'date' : '2018-11-07 13:12:26','foo': 1,'bar':   100},
{'date' : '2019-01-20 06:30:39','foo': 1,'bar':   100},
{'date' : '2018-11-05 21:21:29','foo': 0,'bar':   80},
{'date' : '2018-11-06 09:01:39','foo': 0,'bar':100 }])

print(df)
print(df.dtypes)

# Parse date column
df['date'] = pd.to_datetime(df['date'])
print(df)
print(df.dtypes)

df['date_day'] = df['date'].dt.day

# Select based on day
print(df[df['date_day'] == 20])

If you can clarify your question further it should be possible to provide you with a more specific example. Please also take into account that some steps in the above example can be combined... (part of showing an option).

More information on working with dates can be found in (among others) following resources:

Hope this helps.

Upvotes: 1

Related Questions