Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Python timedelta time difference from the given varied time of every day

I have the these two following pandas dataframe :

df1 = {'Date' : ['07-10-2019',
              '07-10-2019',
              '07-10-2019',
              '08-10-2019',
              '08-10-2019',
              '08-10-2019']}

df1 = {'Time' :['07-10-2019 10:47:00',
              '07-10-2019 10:52:00',
              '07-10-2019 10:59:00',
              '08-10-2019 10:47:00',
              '08-10-2019 10:52:00',
              '08-10-2019 10:59:00',
]}

and I am trying to create d['Time Taken'] by using:
df1['Time Taken']=((pd.to_datetime(df1['Time'])-pd.to_datetime(df1['Date'])).dt.total_seconds())/60
and clearly as expected, I am getting resultant df1['Time Taken'] as:

df1 = {'Time Taken': [647.0,
                      652.0,
                      659.0,
                      647.0,
                      652.0,
                      659.0,
]}

However, I would like to see df['Time Taken] start with NaN or 0. How should I write it this line code to make it look like:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

df1 = {'Time Taken': [NaN,
                      5.0,
                      7.0,
                      NaN,
                      5.0,
                      7.0,
]}

Please, help me with this! Many thanks!

>Solution :

Use DataFrameGroupBy.diff with Series.dt.total_seconds and divide 60:

df1 = pd.DataFrame({'Date' : ['07-10-2019',
              '07-10-2019',
              '07-10-2019',
              '08-10-2019',
              '08-10-2019',
              '08-10-2019'], 
              'Time' :['07-10-2019 10:47:00',
              '07-10-2019 10:52:00',
              '07-10-2019 10:59:00',
              '08-10-2019 10:47:00',
              '08-10-2019 10:52:00',
              '08-10-2019 10:59:00',
]})

df1['Time']= pd.to_datetime(df1['Time'])


df1['Time Taken'] = df1.groupby('Date')['Time'].diff().dt.total_seconds() / 60
print(df1)
         Date                Time  Time Taken
0  07-10-2019 2019-07-10 10:47:00         NaN
1  07-10-2019 2019-07-10 10:52:00         5.0
2  07-10-2019 2019-07-10 10:59:00         7.0
3  08-10-2019 2019-08-10 10:47:00         NaN
4  08-10-2019 2019-08-10 10:52:00         5.0
5  08-10-2019 2019-08-10 10:59:00         7.0
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading