简体   繁体   中英

How to operate on series objects of pandas dataframe?

My pandas dataframe contains two columns (series objects). They are of the forma H:M:S with datatype series. I want to find an element wise difference between the two timeseries, to find out the length of the day. That is,

H2:M2:S2 - H1:M1:S1

I am new to python. Any suggestions?

Here is my dataframe:

df.head()

UNIXTime    Data    Time    Radiation   Temperature Pressure    Humidity    WindDirection(Degrees)  Speed   TimeSunRise TimeSunSet  
0   1475229326  9/29/2016 12:00:00 AM   23:55:26    1.21    48  30.46   59  177.39  5.62    06:13:00    18:13:00  
1   1475229023  9/29/2016 12:00:00 AM   23:50:23    1.21    48  30.46   58  176.78  3.37    06:13:00    18:13:00  
2   1475228726  9/29/2016 12:00:00 AM   23:45:26    1.23    48  30.46   57  158.75  3.37    06:13:00    18:13:00  
3   1475228421  9/29/2016 12:00:00 AM   23:40:21    1.21    48  30.46   60  137.71  3.37    06:13:00    18:13:00  
4   1475228124  9/29/2016 12:00:00 AM   23:35:24    1.17    48  30.46   62  104.95  5.62    06:13:00    18:13:00  
print(df.dtypes)

UNIXTime                    int64  
Data                       object  
Time                       object  
Radiation                 float64  
Temperature                 int64  
Pressure                  float64  
Humidity                    int64  
WindDirection(Degrees)    float64  
Speed                     float64  
TimeSunRise                object  
TimeSunSet                 object    
dtype: object  


df.TimeSunSet.__dict__
{'_cacher': ('TimeSunSet',  
  <weakref at 0x000000000B5C04F8; to 'DataFrame' at 0x000000000B556438>),  
 '_data': SingleBlockManager  
 Items: RangeIndex(start=0, stop=32686, step=1)  
 ObjectBlock: 32686 dtype: object,  
 '_index': RangeIndex(start=0, stop=32686, step=1),  
 '_item_cache': {},  
 '_name': 'TimeSunSet',  
 '_subtyp': 'series',  
 'is_copy': None}  

I want to find the difference between TimeSunSet - TimeSunRise . That is the difference between the last column and the last but one column. I am using python 3.5.1 on windows anaconda.

I think you need convert columns to_timedelta and then subtract:

df['difference'] = pd.to_timedelta(df['TimeSunSet']) - pd.to_timedelta(df['TimeSunRise'])
print (df)
     UNIXTime                   Data      Time  Radiation  Temperature  \
0  1475229326  9/29/2016 12:00:00 AM  23:55:26       1.21           48   
1  1475229023  9/29/2016 12:00:00 AM  23:50:23       1.21           48   
2  1475228726  9/29/2016 12:00:00 AM  23:45:26       1.23           48   
3  1475228421  9/29/2016 12:00:00 AM  23:40:21       1.21           48   
4  1475228124  9/29/2016 12:00:00 AM  23:35:24       1.17           48   

   Pressure  Humidity  WindDirection(Degrees)  Speed TimeSunRise TimeSunSet  \
0     30.46        59                  177.39   5.62    06:13:00   18:13:00   
1     30.46        58                  176.78   3.37    06:13:00   18:13:00   
2     30.46        57                  158.75   3.37    06:13:00   18:13:00   
3     30.46        60                  137.71   3.37    06:13:00   18:13:00   
4     30.46        62                  104.95   5.62    06:13:00   18:13:00   

  difference  
0   12:00:00  
1   12:00:00  
2   12:00:00  
3   12:00:00  
4   12:00:00  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM