Home > OS >  Identify Dates in DataFrame - Pandas
Identify Dates in DataFrame - Pandas

Time:12-13

I have a dataframe:

    Datetime
0   2022-06-01 00:00:00 0
1   2022-06-01 00:01:00 0
2   2022-06-01 00:02:00 0
3   2022-06-01 00:03:00 0
4   2022-06-01 00:04:00 0

How to identify the hour is "00", and so for the minutes and seconds. My requirement is to later on like to put them in a function.

CodePudding user response:

You can use:

s = pd.to_datetime(df['Datetime'], format='%Y-%m-%d %H:%M:%S 0') # what is the 0?

df['hour_0'] = s.dt.hour.eq(0)
df['min_0'] = s.dt.minute.eq(0)
df['sec_0'] = s.dt.second.eq(0)

Output:

                Datetime  hour_0  min_0  sec_0
0  2022-06-01 00:00:00 0    True   True   True
1  2022-06-01 00:01:00 0    True  False   True
2  2022-06-01 00:02:00 0    True  False   True
3  2022-06-01 00:03:00 0    True  False   True
4  2022-06-01 00:04:00 0    True  False   True

CodePudding user response:

You can easily extract hours, minutues, seconds directly from date time string. what is extra 0?. If you have extra strings then simply filter first then extra parameters.

df['new'] = pd.to_datetime(df['Datetime'].str.split(' ').str[1],format='%H:%M:%S')
df['hour'] = df['new'].dt.hour
df['minute'] = df['new'].dt.minute
df['second'] = df['new'].dt.second
del df['new']

Gives #

             Datetime      hour  minute  second
0  2022-06-01 00:00:00 0     0       0       0
1  2022-06-01 00:01:00 0     0       1       0
2  2022-06-01 00:02:00 0     0       2       0
3  2022-06-01 00:03:00 0     0       3       0
4  2022-06-01 00:04:00 0     0       4       0

CodePudding user response:

So, your question is a bit unclear to me, but if I understand correctly you just need to extract the hours from your DF? If so the easiest way to do this is to use Pandas inbuilt datetime functionality. For example:

import pandas as pd
df = pd.DataFrame([["2022-12-12 01:59:00"], ["2022-13-12 01:59:00"]])
print(df)

This will yield:

                     0
0  2022-12-12 01:59:00
1   2022-12-13 01:59:00

Now can do:

pd['timestamp'] = pd.to_datetime(df[0])
pd['hour'] = pd['timestamp'].dt.hour

You can do this for minutes and seconds etc. Hope that helps.

  • Related