1

i have a pandas dataframe:

Start_time | Duration(minutes)

2018-03-01 16:37:09 | 155

2018-03-01 07:02:10 | 5

2018-03-01 13:07:09 | 250

2018-03-01 20:46:34 | 180

2018-03-01 07:45:49 | 5

I want output as

Start_time | End time 2018-03-01 16:37:09 | 2018-03-01 19:12:09

2018-03-01 07:02:10 | 2018-03-01 07:07:10

2018-03-01 13:07:09 | 2018-03-01 17:17:09

2018-03-01 20:46:34 | 2018-03-01 23:46:34

2018-03-01 07:45:49 | 2018-03-01 07:50:49

I am using following code and getting the output for 5-10 rows as required with an warning and when I applied same code on full data set it is showing error as **TypeError: Cannot compare type 'Timestamp' with type 'int' **

time_temp['End_time'] = pd.DatetimeIndex(time_temp['Start_time']) + pd.to_timedelta(time_temp['Duration'], unit='m')

Error: Cannot compare type 'Timestamp' with type 'int' Warning: /usr/local/lib/python3.5/dist-packages/ipykernel_launcher.py:1: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame. Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy """Entry point for launching an IPython kernel.

1 Answers1

3

You need change DatetimeIndex to to_datetime for remove first error:

Cannot compare type 'Timestamp' with type 'int' **

time_temp['End_time'] = (pd.to_datetime(time_temp['Start_time']) + 
                         pd.to_timedelta(time_temp['Duration'], unit='m'))
print (time_temp)
            Start_time  Duration            End_time
0  2018-03-01 16:37:09       155 2018-03-01 19:12:09
1  2018-03-01 07:02:10         5 2018-03-01 07:07:10
2  2018-03-01 13:07:09       250 2018-03-01 17:17:09
3  2018-03-01 20:46:34       180 2018-03-01 23:46:34
4  2018-03-01 07:45:49         5 2018-03-01 07:50:49

For avoid second SettingWithCopyWarning obviously need copy if some filtering, because if you modify values in df later you will find that the modifications do not propagate back to the original data (time_temp), and that Pandas does warning:

time_temp = df[some filtering].copy()

There should be another problems, check how to deal with settingwithcopywarning in pandas

jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
  • Thanks.. It is working now. And yaa still I am getting the warning..Thanks for the explanation. – Jahnab Kumar Deka May 13 '18 at 15:25
  • @JahnabKumarDeka - `copy` does not help? There is no filtering before `time_temp['End_time'] = pd.DatetimeIndex(time_temp['Start_time']) + pd.to_timedelta(time_temp['Duration'], unit='m')` ? – jezrael May 13 '18 at 15:26