Jak mogę obliczyć upływające miesiące za pomocą pand? Mam napisać następujące, ale ten kod nie jest elegancki. Czy mógłbyś powiedzieć mi lepszy sposób?Pandy Timedelta w miesiącach
import pandas as pd
df = pd.DataFrame([pd.Timestamp('20161011'),
pd.Timestamp('20161101') ], columns=['date'])
df['today'] = pd.Timestamp('20161202')
df = df.assign(
elapsed_months=(12 *
(df["today"].map(lambda x: x.year) -
df["date"].map(lambda x: x.year)) +
(df["today"].map(lambda x: x.month) -
df["date"].map(lambda x: x.month))))
# Out[34]:
# date today elapsed_months
# 0 2016-10-11 2016-12-02 2
# 1 2016-11-01 2016-12-02 1
W ten sposób elapased_months dtype jest "obiektem", więc konwertuję "obiekt" na "int64" za pomocą pd.to_numeric(). Dziękuję za Twoją odpowiedź. – Keiku