Skip to content

BUG: Plotting with PeriodIndex with frequency > 1 leads to wrong x-axis #51425

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
2 of 3 tasks
gorold opened this issue Feb 16, 2023 · 1 comment
Open
2 of 3 tasks
Labels

Comments

@gorold
Copy link

gorold commented Feb 16, 2023

Pandas version checks

  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • I have confirmed this bug exists on the main branch of pandas.

Reproducible Example

from datetime import datetime, timedelta
import pandas as pd

date_today = datetime(2023, 2, 2, 0, 0)
days = pd.date_range(date_today, date_today + timedelta(minutes=10), freq='5T').to_period()
data = np.random.randint(1, high=100, size=len(days))
df = pd.DataFrame({'index': days, 'data': data}).set_index("index")

display(df)

df.plot()

df.index = df.index.to_timestamp()
df.plot()

Issue Description

Plotting with PeriodIndex with frequencies larger than 1 (e.g. 5T, 60T, ...) plots the x-axis as the end of the period, rather than the start of the period.

The created dataframe:
Screenshot 2023-02-16 at 2 44 15 pm

Plotting directly:
periodindex

Changing to DatetimeIndex:
timestamp
ng)

Expected Behavior

The expected behaviour should be that what is shown for DatetimeIndex.

Installed Versions

INSTALLED VERSIONS

commit : 2e218d1
python : 3.9.12.final.0
python-bits : 64
OS : Linux
OS-release : 5.10.133+
Version : #1 SMP Sun Sep 11 08:45:44 UTC 2022
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : C.UTF-8
LOCALE : en_US.UTF-8

pandas : 1.5.3
numpy : 1.22.3
pytz : 2022.1
dateutil : 2.8.2
setuptools : 67.1.0
pip : 23.0
Cython : None
pytest : None
hypothesis : 6.56.4
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : 2.9.5
jinja2 : 3.1.2
IPython : 8.4.0
pandas_datareader: None
bs4 : 4.11.1
bottleneck : None
brotli :
fastparquet : None
fsspec : 2023.1.0
gcsfs : None
matplotlib : 3.6.3
numba : 0.56.4
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : 11.0.0
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : 1.10.0
snappy : None
sqlalchemy : 2.0.2
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
zstandard : None
tzdata : None

@gorold gorold added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Feb 16, 2023
@gorold gorold changed the title BUG: BUG: PeriodIndex with frequency > 1 leads to wrong x-axis Feb 16, 2023
@gorold gorold changed the title BUG: PeriodIndex with frequency > 1 leads to wrong x-axis BUG: Plotting with PeriodIndex with frequency > 1 leads to wrong x-axis Feb 16, 2023
@phofl phofl added Visualization plotting and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Feb 23, 2023
@azjps
Copy link

azjps commented Dec 26, 2023

Here's the source of the issue if someone wants to follow up:

> pandas/pandas/plotting/_matplotlib/timeseries.py(311)maybe_convert_index()
    309             if isinstance(data.index, ABCDatetimeIndex):
    310                 data = data.tz_localize(None).to_period(freq=freq_str)
--> 311             elif isinstance(data.index, ABCPeriodIndex):
    312                 data.index = data.index.asfreq(freq=freq_str)
    313     return data

ipdb>  p data.index.asfreq(freq=freq_str)
PeriodIndex(['2023-02-02 00:04', '2023-02-02 00:09', '2023-02-02 00:14'], dtype='period[min]', name='index')
ipdb>  p data.index
PeriodIndex(['2023-02-02 00:00', '2023-02-02 00:05', '2023-02-02 00:10'], dtype='period[5min]', name='index')
ipdb>  p freq_str
'min'

Tagging as tangentially related to #52895

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants