python - Formatting a pandas datetime object from [HH:MM:ss] to [HH:MM] -


i have pandas dataframe 4 columns (time,t1,t2,t3) , respective values obtained throughout day using custom made temperature data logger. time column has format [hh:mm:ss] change [hh:mm] (truncate seconds) plotting purposes. there simple way achieve this?

here code: (using python 3.6)

import pandas pd   import matplotlib.pyplot plt   df = pd.dataframe(pd.read_excel('water_data_full_load.xlsx'))   df.drop("date", axis = 1, inplace =true)   df.set_index('time',inplace =true)    df.rename_axis({"t1": "twall", "t2":"twater", "t3":"tsurr"}, axis=1, inplace=true)  df['time'] = df['time'].apply(lambda x: x[:5])   graph = df[['twall','twater','tsurr']].plot()   plt.xticks(rotation =45)   plt.ylabel('temperature ($^\circ$c)')   plt.xlabel('time of day (hh:mm)')   plt.show(graph) 

please refer image current dataframe

enter image description here

update:

in [41]: df = pd.dataframe({'time': ['11:07:00','12:06:00','13:17:00'], 'twall':[10,20,30]}).set_index('time')  in [42]: df out[42]:           twall time 11:07:00     10 12:06:00     20 13:17:00     30  in [43]: df.index = df.index.astype(str).str.rsplit(':',n=1).str[0]  in [44]: df out[44]:        twall time 11:07     10 12:06     20 13:17     30 

demo (vectorized approach) - truncating seconds column of datetime dtype:

in [46]: df = pd.dataframe(pd.date_range('2017-01-01', freq='99s', periods=10), columns=['date'])  in [47]: df out[47]:                  date 0 2017-01-01 00:00:00 1 2017-01-01 00:01:39 2 2017-01-01 00:03:18 3 2017-01-01 00:04:57 4 2017-01-01 00:06:36 5 2017-01-01 00:08:15 6 2017-01-01 00:09:54 7 2017-01-01 00:11:33 8 2017-01-01 00:13:12 9 2017-01-01 00:14:51   in [49]: df['date'] = df['date'].values.astype('<m8[m]')  in [50]: df out[50]:                  date 0 2017-01-01 00:00:00 1 2017-01-01 00:01:00 2 2017-01-01 00:03:00 3 2017-01-01 00:04:00 4 2017-01-01 00:06:00 5 2017-01-01 00:08:00 6 2017-01-01 00:09:00 7 2017-01-01 00:11:00 8 2017-01-01 00:13:00 9 2017-01-01 00:14:00 

Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -