简体   繁体   English

Python pandas 为周和年以及月和年创建日期时间

[英]Python pandas creating datetime for Week and Year and Month and Year

I'm have a pandas dataframe with: Branch, Year, Month, Week, Assignments, Sales Volume我有一个 pandas dataframe 有:分支,年,月,周,分配,销量

picture:图片: 数据框

I'm trying to display multiple years on my plotly dashboard based and Month.Year and Week.Year我试图在我的 plotly 仪表板和 Month.Year 和 Week.Year 上显示多年

like on this picture I'm dispaying two years with Year Month on the Xaxis:就像在这张照片上,我在 Xaxis 上用 Year Month 显示两年: 应该怎么样!

on this picture you can see that this years are ordered in a timeline from 2018-2019.在这张图片上,您可以看到今年是按照 2018-2019 年的时间线排序的。

My problem im trying to create datetime based and week.year = (52.2018) and month.year = (12.2019) but I cant create datetimes like week.year or month.year im getting from this code:我的问题是我试图创建基于日期时间和 week.year = (52.2018) 和 month.year = (12.2019) 但我无法创建像 week.year 或 month.year 这样的日期时间我从这段代码中得到:

dff['monthYear'] = pd.to_datetime(dff.Monat.astype(str) + '-' + dff.Jahr.astype(str), format='%m-%y')

This error:这个错误:

File "C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\tools\datetimes.py", line 456, in _convert_listlike_datetimes values, tz = conversion.datetime_to_datetime64(arg)文件“C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\tools\datetimes.py”,第 456 行,在 _convert_listlike_datetimes 值中,tz = conversion.datetime_to_datetime64(arg)

File "pandas_libs\tslibs\conversion.pyx", line 350, in pandas._libs.tslibs.conversion.datetime_to_datetime64文件“pandas_libs\tslibs\conversion.pyx”,第 350 行,在 pandas._libs.tslibs.conversion.datetime_to_datetime64

TypeError: Unrecognized value type: <class 'str'> TypeError:无法识别的值类型:<class 'str'>

During handling of the above exception, another exception occurred:在处理上述异常的过程中,又出现了一个异常:

Traceback (most recent call last):回溯(最近一次通话最后):

File "C:\Users\User\Desktop\DashProjekt\Jonen\ProjektBA\MultiYeartest.py", line 24, in dff['monthYear'] = pd.to_datetime(dff.Monat.astype(str) + '-' + dff.Jahr.astype(str), format='%m-%y')文件“C:\Users\User\Desktop\DashProjekt\Jonen\ProjektBA\MultiYeartest.py”,第 24 行,在 dff['monthYear'] = pd.to_datetime(dff.Monat.astype(str) + '-' + dff.Jahr.astype(str), 格式='%m-%y')

File "C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\tools\datetimes.py", line 801, in to_datetime cache_array = _maybe_cache(arg, format, cache, convert_listlike)文件“C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\tools\datetimes.py”,第 801 行,在 to_datetime cache_array = _maybe_cache(arg, format, cache, convert_listlike)

File "C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\tools\datetimes.py", line 178, in _maybe_cache cache_dates = convert_listlike(unique_dates, format)文件“C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\tools\datetimes.py”,第 178 行,在 _maybe_cache cache_dates = convert_listlike(unique_dates, format)

File "C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\tools\datetimes.py", line 460, in _convert_listlike_datetimes raise e文件“C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\tools\datetimes.py”,第 460 行,在 _convert_listlike_datetimes raise e

File "C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\tools\datetimes.py", line 423, in _convert_listlike_datetimes result, timezones = array_strptime(文件“C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\tools\datetimes.py”,第 423 行,在 _convert_listlike_datetimes 结果中,timezones = array_strptime(

File "pandas_libs\tslibs\strptime.pyx", line 150, in pandas._libs.tslibs.strptime.array_strptime文件“pandas_libs\tslibs\strptime.pyx”,第 150 行,在 pandas._libs.tslibs.strptime.array_strptime

ValueError: unconverted data remains: 20 ValueError:未转换的数据仍然存在:20

Thank you in advance.先感谢您。

Greeting LittleStudent问候小学生

  1. concatenate as strings with '-''-'连接成字符串
  2. convert to datetime转换为日期时间
  3. reformat the datetime in plotly重新格式化 plotly 中的日期时间
df.head()
###
   Year  Month  Sales Volume
0  2020      1    773.956049
1  2020      2    438.878440
2  2020      3    858.597920
3  2020      4    697.368029
4  2020      5     94.177348


df['ym'] = df['Year'].astype(str) + '-' + df['Month'].astype(str)
df['ym'] = pd.to_datetime(df['ym'], format='%Y-%m')

fig = px.bar(df, x='ym', y='Sales Volume', color=df['Year'].astype(str))
fig.update_xaxes(tickformat='%Y_%m', tickangle=45)
fig.show()

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM