Skip to content

BUG: incorrect output of first('1M') in case if first index is the last day of the month (#29623) #30138

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 5 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
5 changes: 5 additions & 0 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -8550,6 +8550,11 @@ def first(self, offset):
return self

offset = to_offset(offset)

# Check if offset and index[0] overlap
Copy link
Contributor

Choose a reason for hiding this comment

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

looks like you simply want to set end = index[0], so do that

if offset.onOffset(self.index[0]):
return self.loc[:self.index[0]]

end_date = end = self.index[0] + offset

# Tick-like, e.g. 3 weeks
Expand Down
13 changes: 10 additions & 3 deletions pandas/tests/generic/test_generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -912,7 +912,7 @@ def test_equals(self):

def test_pipe(self):
df = DataFrame({"A": [1, 2, 3]})
f = lambda x, y: x ** y
def f(x, y): return x ** y
Copy link
Contributor

Choose a reason for hiding this comment

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

don't change unrelated things

result = df.pipe(f, 2)
expected = DataFrame({"A": [1, 4, 9]})
tm.assert_frame_equal(result, expected)
Expand All @@ -922,7 +922,7 @@ def test_pipe(self):

def test_pipe_tuple(self):
df = DataFrame({"A": [1, 2, 3]})
f = lambda x, y: y
def f(x, y): return y
result = df.pipe((f, "y"), 0)
tm.assert_frame_equal(result, df)

Expand All @@ -931,7 +931,7 @@ def test_pipe_tuple(self):

def test_pipe_tuple_error(self):
df = DataFrame({"A": [1, 2, 3]})
f = lambda x, y: y
def f(x, y): return y
with pytest.raises(ValueError):
df.pipe((f, "y"), x=1, y=0)

Expand Down Expand Up @@ -970,3 +970,10 @@ def test_deprecated_get_dtype_counts(self):
df = DataFrame([1])
with tm.assert_produces_warning(FutureWarning):
df.get_dtype_counts()

# test case where offset and index[0] overlap
Copy link
Contributor

Choose a reason for hiding this comment

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

move this near other tests for first: look in pandas/tests/frame/test_timeseries and series....

def test_first_index_series_last_day_of_month(self):
series = pd.Series(1, index=pd.bdate_range('2010-03-31', periods=100))
res = series.first('1M')
assert("2010-03-31" in res)
Copy link
Contributor

Choose a reason for hiding this comment

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

we need a full comparison using assert_series_equal

assert("2010-04-30" not in res)
Copy link
Contributor

Choose a reason for hiding this comment

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

parameterize over first/last

Binary file removed pandas/tests/io/sas/data/DEMO_G.xpt
Binary file not shown.
Binary file removed pandas/tests/io/sas/data/DRXFCD_G.xpt
Binary file not shown.