2017-10-01 253 views
3

尋找一種方法來計算python中以下日期/時間格式的年齡。從python/pandas中的日期/時間格式計算年齡

例如:1956-07-01T00:00:00Z

我已經寫了代碼通過提取字符串的四個字符,將其轉換爲int和2017減去它這樣做,但一直在尋找看看是否有一個有效的方法來做到這一點。

+0

可能重複的[熊貓得到的日期(例如:出生日期)](https://stackoverflow.com/questions/26788854/pandas-get-the-age-from-a- date-example-date-date) –

回答

4

這是你想要的嗎?

(pd.to_datetime('today').year-pd.to_datetime('1956-07-01').year) 

Out[83]: 61 
+0

哦,是的!感謝您的快速回復。 – Nivi

+0

@Nivi,歡迎來到SO。如果Wen已經充分回答了您的問題,您應該通過檢查向下箭頭下面的空心複選標記符號來將其標記爲已接受。 – DJK

+0

我可以知道downvote的原因嗎? – Wen

1

我由365.25

(pd.to_datetime('today') - pd.to_datetime('1956-07-01')).days/365.25 

61.24845995893224 
+0

爲什麼使用365.25? – DJK

+0

每年的平均天數 – piRSquared

0

劃分經由timedelta對象的天數如果有如下的不規則年(例如1601),pd.to_datetime將是一個誤差。

import pandas as pd 

(pd.to_datetime('today').year-pd.to_datetime('1601-07-01').year) 

# Traceback (most recent call last): 
# File "/home/kuroyanagi/.pyenv/versions/anaconda3-4.4.0/lib/python3.6/site-packages/pandas/core/tools/datetimes.py", line 444, in _convert_listlike 
#  values, tz = tslib.datetime_to_datetime64(arg) 
# File "pandas/_libs/tslib.pyx", line 1810, in pandas._libs.tslib.datetime_to_datetime64 (pandas/_libs/tslib.c:33275) 
# TypeError: Unrecognized value type: <class 'str'> 
# During handling of the above exception, another exception occurred: 
# Traceback (most recent call last): 
# File "/home/kuroyanagi/.pyenv/versions/anaconda3-4.4.0/lib/python3.6/site-packages/IPython/core/interactiveshell.py", line 2881, in run_code 
#  exec(code_obj, self.user_global_ns, self.user_ns) 
# File "<ipython-input-45-829e219d9060>", line 1, in <module> 
#  (pd.to_datetime('today').year-pd.to_datetime('1601-07-01').year) 
# File "/home/kuroyanagi/.pyenv/versions/anaconda3-4.4.0/lib/python3.6/site-packages/pandas/core/tools/datetimes.py", line 518, in to_datetime 
#  result = _convert_listlike(np.array([arg]), box, format)[0] 
# File "/home/kuroyanagi/.pyenv/versions/anaconda3-4.4.0/lib/python3.6/site-packages/pandas/core/tools/datetimes.py", line 447, in _convert_listlike 
#  raise e 
# File "/home/kuroyanagi/.pyenv/versions/anaconda3-4.4.0/lib/python3.6/site-packages/pandas/core/tools/datetimes.py", line 435, in _convert_listlike 
#  require_iso8601=require_iso8601 
# File "pandas/_libs/tslib.pyx", line 2355, in pandas._libs.tslib.array_to_datetime (pandas/_libs/tslib.c:46617) 
# File "pandas/_libs/tslib.pyx", line 2538, in pandas._libs.tslib.array_to_datetime (pandas/_libs/tslib.c:45511) 
# File "pandas/_libs/tslib.pyx", line 2506, in pandas._libs.tslib.array_to_datetime (pandas/_libs/tslib.c:44978) 
# File "pandas/_libs/tslib.pyx", line 2500, in pandas._libs.tslib.array_to_datetime (pandas/_libs/tslib.c:44859) 
# File "pandas/_libs/tslib.pyx", line 1517, in pandas._libs.tslib.convert_to_tsobject (pandas/_libs/tslib.c:28598) 
# File "pandas/_libs/tslib.pyx", line 1774, in pandas._libs.tslib._check_dts_bounds (pandas/_libs/tslib.c:32752) 
# pandas._libs.tslib.OutOfBoundsDatetime: Out of bounds nanosecond timestamp: 1601-07-01 00:00:00 

對於包含非正常年份的數據,您可以計算如下。

import numpy as np 
import pandas as pd 

date = pd.Series(['1601-07-01', '1956-07-01']) 

def elasped_years(date): 
    reference_year = pd.to_datetime('today').year 
    reference_month = pd.to_datetime('today').month 
    year = date.str.slice(0, 4).astype(np.float) 
    month = date.str.slice(5, 7).astype(np.float) 
    duration = np.floor((12 * (reference_year - year) + (reference_month - month))/12) 
    return(duration) 

elasped_years(date) 
# Out[46]: 
# 0 416.0 
# 1  61.0 
# dtype: float64