Anh Phuong Bui
Anh Phuong Bui

Reputation: 61

Python replace string data

I am a Python beginner doing simple data cleaning.

I am stuck on this problem: I have a dataframe:

  datetime
1/1/2012 00:05 
1/1/2012 00:10 
1/1/2012 00:15
1/1/2012 00:20
1/1/2012 00:25
1/1/2012 00:30

... continuing with 5 min frequency till the end of the year.

I am trying to change 5 continuous rows to the value of every 6th row.

Essentially, it should look like this:

1/1/2012 00:30 
1/1/2012 00:30 
1/1/2012 00:30 
1/1/2012 00:30 
1/1/2012 00:30 
1/1/2012 00:30 
1/1/2012 01:00 
1/1/2012 01:00 
1/1/2012 01:00 
1/1/2012 01:00 
1/1/2012 01:00 
1/1/2012 01:00 

How can I do this? Thanks

Upvotes: 0

Views: 87

Answers (2)

user3483203
user3483203

Reputation: 51155

Setup

df = pd.DataFrame({'datetime': ['1/1/2012 00:05', '1/1/2012 00:10', '1/1/2012 00:15', '1/1/2012 00:20', '1/1/2012 00:25', '1/1/2012 00:30', '1/1/2012 00:35']})

pd.to_datetime with dt.ceil

df['datetime'] = pd.to_datetime(df['datetime']).dt.ceil('30min')

                 date
0 2012-01-01 00:30:00
1 2012-01-01 00:30:00
2 2012-01-01 00:30:00
3 2012-01-01 00:30:00
4 2012-01-01 00:30:00
5 2012-01-01 00:30:00
6 2012-01-01 01:00:00

Upvotes: 2

gilch
gilch

Reputation: 11651

You can step with slice notation, e.g.

>>> spam = tuple(range(30))
>>> spam
(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29)
>>> spam[::6]
(0, 6, 12, 18, 24)
>>> spam[::10]
(0, 10, 20)
>>> spam[::5]
(0, 5, 10, 15, 20, 25)
>>> spam[1::5]
(1, 6, 11, 16, 21, 26)
>>> spam[2::5]
(2, 7, 12, 17, 22, 27)

Upvotes: 0

Related Questions