pandas: force 'minute' and 'seconds' to be zero

Viewed 1821

I have one column: line1 [ 'daytime' ]

the format of this column looks like:

2018-02-07 17:40:29
2018-02-07 17:41:15
2018-02-07 17:41:55
2018-02-07 17:42:54
2018-02-07 17:43:44
2018-02-07 18:02:54
2018-02-07 18:03:44
Name: daytime, Length: 174859, dtype: datetime64[ns]

I want to get:

2018-02-07 17:00:00
2018-02-07 17:00:00
2018-02-07 17:00:00
2018-02-07 17:00:00
2018-02-07 17:00:00
2018-02-07 18:00:00
2018-02-07 18:00:00

I want change the whole column

4 Answers

Use astype to cast to a numpy with unit as hour

df.daytime.astype('datetime64[h]')

#                 dates
# 0 2018-02-07 17:00:00
# 1 2018-02-07 17:00:00
# 2 2018-02-07 17:00:00
# 3 2018-02-07 17:00:00
# 4 2018-02-07 17:00:00
# 5 2018-02-07 18:00:00
# 6 2018-02-07 18:00:00

Some speed comparisons between the solutions provided:

datetime = pd.date_range(start='2020-01-01', freq='200S', periods=100)
df = pd.DataFrame(dict(daytime=datetime))

%%timeit
df.daytime.dt.to_period('H')
# 826 µs ± 355 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)

%%timeit
x = df.daytime.dt.floor('H')
# 774 µs ± 247 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)

%%timeit
df.daytime.astype('datetime64[h]')
# 190 µs ± 12.7 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)

With 1k records:

datetime = pd.date_range(start='2020-01-01', freq='200S', periods=1000)
df = pd.DataFrame(dict(daytime=datetime))

%%timeit
df.daytime.dt.to_period('H')
# 991 µs ± 312 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)

%%timeit
x = df.daytime.dt.floor('H')
# 825 µs ± 203 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)

%%timeit
df.daytime.astype('datetime64[h]')
# 237 µs ± 8.02 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)

There is a floor method of pandas Timestamps for doing just this. Where x is an array (or column of a DataFrame):

x = pd.to_datetime(x).dt.floor('H')

For Series or for single timestamps.

For a unique Timestamp :

You can use replace :

pd.Timestamp('2018-02-07 17:40:29').replace(minute=0, second=0)

This will output :

Timestamp('2018-02-07 17:00:00')

For a pd.Series:

df['your_column'].dt.to_period('H')
    your_column
0   2018-02-07 17:00
1   2018-02-07 17:00
2   2018-02-07 17:00
3   2018-02-07 17:00
4   2018-02-07 17:00

Here is how you can use the module re:

import re

date = "2018-02-07 17:40:29"

print(re.sub('(?<=\d\d:)\d\d:\d\d', '00:00',date))

Output:

2018-02-07 17:00:00

Apply this to each element of the column using a for loop or a list comprehension.

Related