Home > front end >  How to count days using datetime64[ns] with pandas?
How to count days using datetime64[ns] with pandas?

Time:12-04

Uni student here, I have to analyze data for a Data Analytics course. We have to work on a dataset about expeditions on mountains. I want to calculate how many days it took for the climbers to go from the basepoint to the highpoint (expeditions['basecamp_date']&expeditions['highpoint_date']). I found a way to count the days but how can I put those values in a new column? (expeditions['days_reach_top']). I want to be able to calculate the mean etc afterwards. Here is the code I have right now:

expeditions['basecamp_date']=pd.to_datetime(expeditions['basecamp_date'])
expeditions['highpoint_date']=pd.to_datetime(expeditions['highpoint_date'])
expeditions['termination_date']=pd.to_datetime(expeditions['termination_date'])

date_format = "%Y-%m-%d/ns"

a = datetime.strptime(str(expeditions['basecamp_date']), date_format)
b = datetime.strptime(str(expeditions['highpoint_date']), date_format)
delta = b - a
print (delta.days)

expeditions['days_reach_top']

I know it's not complete but it's giving me ValueError("unconverted data remains: %s" % and ValueError("time data %r does not match format %r" %. How can I fix the time format and make calculus with it? For context here's what my dataset looks like: enter image description here

Thanks for the help!

CodePudding user response:

Try to replace

a = datetime.strptime(str(expeditions['basecamp_date']), date_format)
b = datetime.strptime(str(expeditions['highpoint_date']), date_format)

with

a = expeditions['basecamp_date']
b = expeditions['highpoint_date']
  • Related