Chandra
Chandra

Reputation: 524

Pandas increment time series by one minute

My data is here.

I want to add a minute to values in STA_STD to get a 5-minute regular time series, if the value in that column contains "23:59:00". Adding one minute should also change to date to next day 00:00 hours.

My code is here

 dat=pd.read_csv("temp.csv")
 if(dat['STA_STD'].str.contains("23:59:00")):
     dat['STA_STD_NEW']= pd.to_datetime(dat[dat['STA_STD'].str.contains("23:59:00")]         ['STA_STD'])+datetime.timedelta(minutes=1)
 else:
     dat['STA_STD_NEW'] = dat['STA_STD']

And this gives me below error:

 ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

Pandas documentation here talks about the same error.

What is the procedure to iterate through all rows and increment value by one minute, if value contains "23:59:00"?

Please advise.

Upvotes: 0

Views: 2831

Answers (1)

joris
joris

Reputation: 139142

Two things:

  • You can't use if/else like that to evaluate multiple values at the same time (you would need to iterate over the values, and then do the if/else for each value separately). But using boolean indexing is much better in this case.
  • the str.contains does not work with datetimes. But you can eg check if the time part of the datetime values is equal to datetime.time(23, 59)

A small example:

In [2]: dat = pd.DataFrame({'STA_STD':pd.date_range('2012-01-01 23:50', periods=10, freq='1min')})

In [3]: dat['STA_STD_NEW'] = dat['STA_STD']

In [4]: dat.loc[dat['STA_STD'].dt.time == datetime.time(23,59), 'STA_STD_NEW'] += datetime.timedelta(minutes=1)

In [5]: dat
Out[5]:
              STA_STD         STA_STD_NEW
0 2012-01-01 23:50:00 2012-01-01 23:50:00
1 2012-01-01 23:51:00 2012-01-01 23:51:00
2 2012-01-01 23:52:00 2012-01-01 23:52:00
3 2012-01-01 23:53:00 2012-01-01 23:53:00
4 2012-01-01 23:54:00 2012-01-01 23:54:00
5 2012-01-01 23:55:00 2012-01-01 23:55:00
6 2012-01-01 23:56:00 2012-01-01 23:56:00
7 2012-01-01 23:57:00 2012-01-01 23:57:00
8 2012-01-01 23:58:00 2012-01-01 23:58:00
9 2012-01-01 23:59:00 2012-01-02 00:00:00    <-- increment of 1 minute 

Using the dt.time approach needs pandas >= 0.15

Upvotes: 1

Related Questions