1

I have two different dates that I am pulling from a database using a SQL query. Im looking to do transformations in Python, but the two main dates I want to work with are stored in different formats. The first date is of the date format (YYYY/MM/DD) the other is of (YYYY/MM/DD HH:MM:SS) format. I want a difference in days so the DATETIME is irrelevant on the second date. I was wondering what is the easiest way to do this in python? Ideally, I would like to automate this, where I create a DATE format of the DATETIME variable, and take the difference between the two DATES.

I've tried the following but I am also getting errors since I am dealing with Series. I am trying to get the delta for every row.

df.delta = (df.DATETIME - df.DATE)

and

df.delta = datetime.timedelta(df.DATETIME - df.DATE)
mitch
  • 339
  • 3
  • 12
  • 1
    *I've tried the following but I am also getting errors since I am dealing with Series* - what are those errors - can you provide sample data you're using? – Jon Clements Feb 05 '18 at 16:46
  • the most common error i get is the following: "unsupported type for timedelta seconds component: Series" as well as "cannot convert the series to " – mitch Feb 05 '18 at 16:48
  • Perhaps try `df.delta = df.apply(lambda x: (x['DATETIME'] - x['DATE']).days, axis=1)` – pault Feb 05 '18 at 16:49
  • Have you tried converting the datetime to date? https://stackoverflow.com/questions/3743222/how-do-i-convert-datetime-to-date-in-python – Sam Feb 05 '18 at 16:49
  • I have tried, but most solutions i've seen deal with an "object" not a "Series" hence i get the following error when trying that: descriptor 'date' requires a 'datetime.datetime' object but received a 'Series' – mitch Feb 05 '18 at 16:52

2 Answers2

1
import datetime

d1 = datetime.datetime.strptime('2018/01/13', '%Y/%m/%d')
d2 = datetime.datetime.strptime('2018/01/15 18:34:02', '%Y/%m/%d %H:%M:%S')
delta = d2 - d1
print delta.total_seconds()
print delta.days
ababak
  • 1,282
  • 1
  • 9
  • 20
  • Edited to show days. I was not aware he wanted to use pandas – ababak Feb 05 '18 at 16:55
  • I have tried this solution in the past, but what i cannot get past is how to apply this to an entire series, the above gives me the following: strptime() argument 1 must be string, not Series – mitch Feb 05 '18 at 16:58
  • Please explain your solution in English. Someone who understands the code will not benefit as much from an answer (as it is trivial to lookup, *if* you know what to look for). – jpaugh Feb 05 '18 at 18:35
  • I appreciate everyone's help, and I get your point @jpaugh. I was having a problem truly understanding my issue as It turned out to be a combination of things. In the end i slighlty modified the code in the above solution and I got the appropriate formats. – mitch Feb 05 '18 at 21:51
  • @mitch I wasn't talking to you, so I'm not sure what you mean. However, I'm glad you got a resolution. – jpaugh Feb 06 '18 at 17:58
0

Convert your datetime object to a date object, you are then able to subtract them for a delta value.

df.delta = (df.DATETIME.date() - df.DATE)
Sam
  • 404
  • 3
  • 16