简体   繁体   中英

Pandas - Calculate with datetime column with month and year only

I struggle with a column in a dataframe with only should include month and year.

df["Datum"] = pd.to_datetime(df["Datum"], format="%d.%m.%Y").dt.date
df["Month"] = pd.to_datetime(df['Datum']).dt.strftime('%B-%Y')

I use this column as input for my streamlit app like this:

start_date = df["Month"].min()
end_date = df["Month"].max()

start, end = st.sidebar.slider("Label", start_date, end_date, (start_date, end_date))

Pandas seems to handle dt.strftime('%B-%Y') as a string with won´t allow me to do calculations with it.

TypeError: unsupported operand type(s) for -: 'str' and 'str'

st.write(df["Month"].max() - df["Month"].min())

will also fail with the same error.

How can I use this specific format for calculations?

How can I use this specific format for calculations?

If need working with datetimes in python, pandas, it means cannot convert ouput to strings by strftime .

So need convert to custom string in write function:

df["Datum"] = pd.to_datetime(df["Datum"], format="%d.%m.%Y").dt.date
#if need datetiems without times
#df["Datum"] = pd.to_datetime(df["Datum"], format="%d.%m.%Y").dt.normalize()

#if need working with years and months only convert datetimes to month periods
#df["Datum"] = pd.to_datetime(df["Datum"], format="%d.%m.%Y").dt.to_period('m')

start_date = df["Datum"].min()
end_date = df["Datum"].max()

start, end = st.sidebar.slider("Label", start_date.strftime('%B-%Y'), end_date.strftime('%B-%Y'), (start_date.strftime('%B-%Y'), end_date.strftime('%B-%Y')))

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