add support for Pandas Time spans as index col (PeriodIndex)
Chưa có ai nhận issue này.
- Ngôn ngữ chính
- Python
- Star
- 18.8k
- Fork
- 2.8k
- Merge trung bình
- 16 giờ 26 phút
- Pull request đã merge (30 ngày)
- 21
Mô tả
Hello,
I'd like to use the pandas PeriodIndex date format to work with and calculate things in a time series way.
The manual says that Plotly auto-sets the axis type to a date format when the corresponding data are either ISO-formatted date strings or if they're a date pandas column or datetime NumPy array. (https://plotly.com/python/time-series/)
So I guess, since a PeriodIndex isn't of the concept "date" but of the concept "Time spans", Plotl.ly isn't able to auto-set this axis type? The error I get is a TypeError: Object of type Period is not JSON serializable
I know I can solve this with converting to another format (#df.index = df.index.astype(str)), but it would be great to have a native solution inside plot.ly that can understand PeriodIndex.
Here is my example code:
import pandas as pd
import plotly.express as px
### READ IN DATA ###
d = {
'one':
pd.Series([1., 2.],
index=[
'Jan/2020', 'Feb/2020',
])
}
df = pd.DataFrame(d)
print(f'\n# this is what I get from my datasource\n')
print(df.index)
print(df)
### CONVERT DATA ###
idx = df.index
df.index = pd.to_datetime(idx, format="%b/%Y").to_period(freq='M')
print(f'\n# after conversion\n')
print(df.index)
df.index.name = 'Monat'
print(df)
df = df.sort_index()
###THIS FIXES IT, BUT :/ ###
#df.index = df.index.astype(str)
fig = px.line(df, x=df.index, y=('one'), title='Testplot')
fig.show()
Hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Hướng nghiên cứu
Trước tiên, hãy chạy bản tái hiện pandas và Plotly được cung cấp, sau đó theo dõi cách PeriodIndex đi đến quá trình tuần tự hóa JSON và so sánh với các chỉ mục ngày tháng và chuỗi được hỗ trợ. Công việc được hoàn tất khi ví dụ được render mà không yêu cầu astype(str) và hành vi của PeriodIndex được bao phủ bởi một bài kiểm thử.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Đánh giá
- Công nghệ
- pandas, python
- Lĩnh vực
- data-visualization
- Loại issue
- Tính năng
- Độ khó
- 3/5
- Thời gian dự kiến
- 1-2 ngày
- Mức độ hoạt động
- Đình trệ
- Độ rõ ràng
- Khá rõ ràng
- Mức phù hợp với người mới
- 35/100