-1

I have two columns, created date & closed date, format as under. I'm required to extract only the date from each column & try to make a difference

0    12/31/2015 11:59:45 PM
1    12/31/2015 11:59:44 PM
2    12/31/2015 11:59:29 PM
3    12/31/2015 11:57:46 PM
4    12/31/2015 11:56:58 PM

I have tried to use str.split command (using space to segregate) to get only the dates from both created date & closing date However, if I try to take the difference I'm getting the following error:

unsupported operand type(s) for -: 'str' and 'str'

a = nyc311['Created Date']
nyc311['Created Date Revised'] = a.str.split('[ ]').str.get(0)

b = nyc311['Closed Date']
nyc311['Closed Date Revised'] = b.str.split('[ ]').str.get(0)

nyc311['Request_Closing_Time'] = nyc311['Closed Date Revised'] -nyc311['Created Date Revised']

Created date and closing date came out as expected, however, I need to achieve the time lag from created date to the closing date, which is not possible as mentioned because of the error message. This may be a silly question, I'm new to python. Help would be much appreciated.

Ravi B
  • 1,574
  • 2
  • 14
  • 31
Sid
  • 163
  • 7

4 Answers4

1

I think some already asked this question and got an answer, you can look here: How to calculate number of days between two given dates?

so if I understand correctly you need a code to get the difference between two dates or times and not realy how to split the string right ?

you can check this function:

   from datetime import datetime

   def _calculate_range(self, t1, t2):
    """ calculate time range between two different dates """
      try:

        fmt = '%Y-%m-%d %H:%M:%S'
        d1 = datetime.strptime(t1, fmt)
        d2 = datetime.strptime(t2, fmt)
        d1_ts = time.mktime(d1.timetuple())
        d2_ts = time.mktime(d2.timetuple())
        res = int(d2_ts - d1_ts) / 3600
        return res

what you need is to convert your string into a datetime object because you cannot get the difference between two strings, you can achieve that with the strptime method. you need to import datetime so you can use that method

basilisk
  • 1,156
  • 1
  • 14
  • 34
1

It's because your dates are in the string format. You have to convert them to datetime. Here is an example of how it works :

import datetime

df = pd.DataFrame({'Created Date' : ['12/30/2015 11:59:45 PM', '01/10/2016 11:59:45 PM'],
                  'Closed Date' : ['12/31/2015 11:59:44 PM', '01/22/2016 11:59:44 PM']})

# First we use the split function and the datetime conversion in a list comprehension
df['Created Date'] = [datetime.datetime.strptime(x.split(' ')[0], '%m/%d/%Y') 
                      for x in df['Created Date']]
df ['Closed Date'] = [datetime.datetime.strptime(x.split(' ')[0], '%m/%d/%Y') 
                      for x in df['Closed Date']]

df['Request closing Date'] = df ['Closed Date'] - df['Created Date']

Output :

  Created Date Closed Date Request closing Date
0   2015-12-30  2015-12-31               1 days
1   2016-01-10  2016-01-22              12 days
vlemaistre
  • 3,301
  • 13
  • 30
  • I tried to adopt this.. However i need to pass this argument to the entire column which is a series, also hours& minutes are not required and can be completely eliminated. I tried to work on this, but couldn't achieve. Any insights? – Sid Jun 28 '19 at 17:44
  • @Sid I edited my answer to do the computations on a whol dataframe. Have a look and tell me if it's what you wanted :) – vlemaistre Jun 29 '19 at 14:50
1

You can use the strptime() function to strip the time and then find the difference between them. Below code will strip the time from the datetime object 'a'.

a = datetime.strptime(a, '%Y-%m-%d')

You can also check this link for more info

Mihran Thalhath
  • 325
  • 2
  • 10
0

Try converting tour datetime columns to actual datetime (instead of strings) and the use .date

nyc311['Created Date'] = pd.to_datetime(nyc311['Created Date'])
nyc311['Closed Date'] = pd.to_datetime(nyc311['Closed Date'])
nyc311['Request_Closing_Time'] = nyc311['Closed Date'].date() -nyc311['Created Date'].date()
Niels Henkens
  • 2,553
  • 1
  • 12
  • 27