Skip to content
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

correction to get time interpolation correct #39886

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions pandas/core/resample.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@
Tick,
)

from pandas.core.reshape.concat import concat
Copy link
Contributor

Choose a reason for hiding this comment

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

your imports are in the wrong order and causing failed checks.

run > isort pandas


_shared_docs_kwargs: Dict[str, str] = {}


Expand Down Expand Up @@ -859,6 +861,11 @@ def interpolate(
Interpolate values according to different methods.
"""
result = self._upsample("asfreq")
if isinstance(result.index, DatetimeIndex):
obj = self._selected_obj
tmp = concat([obj, result]).sort_index().interpolate(method='time')
tmp = tmp[result.index]
result[...] = tmp[~tmp.index.duplicated(keep='first')]
Copy link
Contributor

Choose a reason for hiding this comment

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

you are not following standard code guideines, e.g. use " instead of '.

run > black pandas this will automatically fix these issues for you.

return result.interpolate(
method=method,
axis=axis,
Expand Down
11 changes: 11 additions & 0 deletions pandas/tests/resample/test_datetime_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -1816,3 +1816,14 @@ def test_resample_aggregate_functions_min_count(func):
index=DatetimeIndex(["2020-03-31"], dtype="datetime64[ns]", freq="Q-DEC"),
)
tm.assert_series_equal(result, expected)


def timeseries_interpolation():
Copy link
Contributor

Choose a reason for hiding this comment

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

this will not run as a test. pytest will run all files of the form test_*.py or *_test.py

rename this test_timeseries_interpolation

Copy link
Author

Choose a reason for hiding this comment

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

I think I have done 90 % of the job, now I let maintainers finishing it. It should take them minutes while it would take my hours to polish the PR.

This bug has been reported 3 years ago and nobody from Pandas tried anything while a solution was proposed in the bug report. Now, 3 years after, I have done a PR. I know it is not perfect but it must be very close to what it should be. I hope it will not take 3 more years.

@attack68 It is not against you. I don't want to become a maintainers but just to help at my level.

Copy link
Author

Choose a reason for hiding this comment

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

@attack68 or any one else: feel free to for from my work and to finish the PR.

Copy link
Member

Choose a reason for hiding this comment

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

@oricou the main thing you need to do at this point is rename this function as @attack68 suggested.

I think I have done 90 % of the job

there are 58k lines of code in pandas/core/ and 220k in pandas/tests/. proper testing is important.

dates1 = date_range('2016-08-28', periods=4, freq='21H')
ts1 = Series([21 * i for i in range(4)], dates1, dtype=float)
nb_periods = (21 * 4) // 15
dates2 = date_range('2016-08-28', periods=nb_periods, freq='15H')
expect = Series([15 * i for i in range(nb_periods)], dates2, dtype=float)

result = ts1.resample('15H').interpolate(method='time')
tm.assert_series_equal(result, expect)