Reputation: 13
i want to change the value of a pandas dataframe column with datetime format. Basically i want to add always 20 seconds to a row. Im new to python/pandas so i dont know any ways to solve that issue.
Here is my code so far:
df_date = dataframe['date'] # get date column
df_date = datedf.to_frame() #convert series to df
for index, row in df_date.iterrows():
if (index % 3 == 0):
#minute +1 f. e. from 12:00:40 to 12:01:0
elif (index % 3 == 1) or (index % 3 == 2):
# seconds + 20
# from 12:01:00 to 12:01:20
# or from 12:01:20 to 12:01:40
Dataframe df_date:
original:
0 2017-03-10 01:00:00
1 2017-03-10 01:00:00
2 2017-03-10 01:00:00
3 2017-03-10 01:00:00
4 2017-03-10 01:00:00
...
expected:
0 2017-03-10 01:00:20
1 2017-03-10 01:00:40
2 2017-03-10 01:01:00
3 2017-03-10 01:00:20
4 2017-03-10 01:00:40
...
Any help on this would be appreciated!
Upvotes: 1
Views: 948
Reputation: 862406
Add timedeltas created by np.arange
multiple by 20
with to_timedelta
:
td = pd.to_timedelta(np.arange(1, len(dataframe) + 1) * 20, unit='s')
Or created by timedelta_range
:
td = pd.timedelta_range(0, periods=len(dataframe), freq='20s')
dataframe['date'] = pd.to_datetime(dataframe['date']) + td
print (dataframe)
date
0 2017-03-10 01:00:20
1 2017-03-10 01:00:40
2 2017-03-10 01:01:00
3 2017-03-10 01:01:20
4 2017-03-10 01:01:40
Upvotes: 3