Skip to content

BUG: Line plots aligned from start #57594 #57713

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

Closed
wants to merge 10 commits into from
Closed

Conversation

NoelMT
Copy link

@NoelMT NoelMT commented Mar 3, 2024

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

The cause of this bug is that by default all elements are generated to align to the end of
a period instead of at the start and the period technically does not end at the start of
the last period but rather just before the period subsequent to the last period. To fix this
problem we therefore passed a parameter stating that the elements should be aligned to
the start of the period instead of the end. Parameter "how=S" was added in pandas/plotting/_matplotlib/timeseries.py::maybe_convert_index(),
on the function call data.index.asfreq(freq=freq_str, how = "S").

Additional tests were added to test whether input data match data in plot and one test was merged into one the added test to reduce code duplication.

Gustaf-Larsson and others added 10 commits February 29, 2024 21:02
… the ones in the resulting plot

* Added test_period_range_content which tests if plotted dates are equal to generated dates using period_range
* Added test_date_range_content which tests if plotted dates are equal to generated dates using date_range
* Moved the checking of the plot data to a separate function
* Added more frequencies to be tested
….py to test frequencies with koefficient > 1. Also removed redundant test called test_construction_from_string_monthly since the updated test_construction_from_string covers the same thing.
#2 Refactor: Updated test_construction_from_string::test_period_range…
@NoelMT NoelMT changed the title BUG: Line plots alligned from start #57594 BUG: Line plots aligned from start #57594 Mar 4, 2024
@datapythonista datapythonista added the Visualization plotting label Mar 5, 2024
@datapythonista
Copy link
Member

Thank you for working on this @NoelMT. Do you mind adding plots to this PR (as a comment or in the description) that show how things are rendered before and after your changes please? That would give context to reviewers and it'll make reviewing much easier. Thanks!

@@ -310,7 +310,7 @@ def maybe_convert_index(ax: Axes, data: NDFrameT) -> NDFrameT:
if isinstance(data.index, ABCDatetimeIndex):
data = data.tz_localize(None).to_period(freq=freq_str)
elif isinstance(data.index, ABCPeriodIndex):
data.index = data.index.asfreq(freq=freq_str)
data.index = data.index.asfreq(freq=freq_str, how = "S") #Lineplot alignes to the start of period
Copy link
Contributor

@natmokval natmokval Mar 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please check ruff, ruff-format and codespell errors on line 313?
If you try the following command: pre-commit run --all-file, it should help fix these errors.

@@ -113,34 +119,6 @@ def test_construction_from_string(self, freq_offset, freq_period):
result = period_range(start=end, end=start, freq=freq_period, name="foo")
tm.assert_index_equal(result, expected)

def test_construction_from_string_monthly(self):
Copy link
Contributor

@natmokval natmokval Mar 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for noticing code duplication in pandas/tests/indexes/period/test_period_range.py. I am not sure how it’s related to this bug. Could you please move these changes to separate PR?

@@ -69,6 +70,31 @@ def test_fontsize_set_correctly(self):
for label in ax.get_xticklabels() + ax.get_yticklabels():
assert label.get_fontsize() == 2

@pytest.mark.parametrize("freq", ["20s","3s","60min","5min","7h","4D","8W","11M","2Q","Y", "3Y", "10Y"])
Copy link
Contributor

@natmokval natmokval Mar 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pre-commit run --all-file fails. Could you please check it?

@@ -69,6 +70,31 @@ def test_fontsize_set_correctly(self):
for label in ax.get_xticklabels() + ax.get_yticklabels():
assert label.get_fontsize() == 2

@pytest.mark.parametrize("freq", ["20s","3s","60min","5min","7h","4D","8W","11M","2Q","Y", "3Y", "10Y"])
def test_period_range_content(self, freq):
# Setup your DataFrame as in your scenario
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instead of the comments, e.g. # Setup your DataFrame as in your scenario, could you please just add the number of GitHub issue: # GH#57594

@natmokval
Copy link
Contributor

Thanks @NoelMT for working on this. Seems like it will fix the problem with visualisation of DataFrame if PeriodIndex as index provided.
e.g. for the first scenario:

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)

plot before:

res_7h

plot after:

dfplot_7h_correct

@natmokval
Copy link
Contributor

@NoelMT could you please add a note to doc/source/whatsnew/v3.0.0.rst in the section

Copy link
Contributor

This pull request is stale because it has been open for thirty days with no activity. Please update and respond to this comment if you're still interested in working on this.

@github-actions github-actions bot added the Stale label Apr 10, 2024
@mroeschke
Copy link
Member

Thanks for the pull request, but it appears to have gone stale. If interested in continuing, please merge in the main branch, address any review comments and/or failing tests, and we can reopen.

@mroeschke mroeschke closed this Apr 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants