简体   繁体   中英

days elapsed since certain date python pandas

I'm trying to get a new column with days since a certain date. I tried the following.. but it gives me the error TypeError: unsupported operand type(s) for -: 'datetime.datetime' and 'datetime.date'

import datetime

days_elapsed = []
since_day = datetime.date(2000,11,31)

for i in df.date:
    date = datetime.datetime.strptime(i, "%Y-%m-%d")
    elapsed = (date - since_day).days
    days_elapsed.append(elapsed)

You're subtracting two different types of date. You need to have two dates or two datetimes. It's probably easier to change since_day to a datetime.

since_day = datetime.date(2000,11,31,0,0)

You are indeed using the wrong type of date but the idiomatic way to accomplish what you are trying to do is the following.

import datetime
import pandas as pd

since_day = pd.Timestamp('2000-11-31')

df['Date'] = pd.to_datetime(df.date) #coerce date column to pandas timestamp
df['elapsed'] = (date - since_day).days

Pandas is designed to work with vectorized operations. Try and avoid iterating over a column, but instead try and perform the operation on the entire column. The API will make more sense, and your code will run faster as it enables pandas to dispatch the calculations to the underlying numpy C machinery.

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