Skip to content

BUG: df.plot makes a shift to the right if frequency multiplies to n > 1 #57594

Open
@natmokval

Description

@natmokval

This bug exists on the latest version of pandas and might be related to the bug reported in #51425.

df.plot DataFrame with PeriodIndex works well for frequency with n=1, while fоr frequency that multiplies on n>1 we have a shift to the right.

Reproducible Examples:

if freq="7h"

from pandas import *
import numpy as np

idx = period_range("01/01/2000", freq='7h', periods=4)
df = DataFrame(
    np.array([0, 1, 0, 1]),
    index=idx,
    columns=["A"],
)
df.plot()
print(df)

the plotting starts from 06:00

res_7h

if freq="2h"

idx = period_range("01/01/2000", freq='2h', periods=4)
df = DataFrame(
    np.array([0, 1, 0, 1]),
    index=idx,
    columns=["A"],
)
df.plot()
print(df)

we have a shift on +1 hour

res_2h

But if freq="h"

idx = period_range("01/01/2000", freq='h', periods=4)
df = DataFrame(
    np.array([0, 1, 0, 1]),
    index=idx,
    columns=["A"],
)
df.plot()
print(df)

the result looks okay:

res_h

Expected Behavior:

I think for both freq="7h" and freq="2h" we should start plotting from the point (00:00, 0.0) as we do for freq="h"

In case DataFrame with DatetimeIndex plotting works correct, e.g.

idx = date_range("01/01/2000", freq='7h', periods=4)
df = DataFrame(
    np.array([0, 1, 0, 1]),
    index=idx,
    columns=["A"],
)
df.plot()
print(df)

output:
res_7h_dr

Metadata

Metadata

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions