Skip to content

BUG: No error raised in to_stata() for -np.inf #45350 #45360

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

Merged
merged 3 commits into from
Jan 16, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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: 3 additions & 2 deletions pandas/io/stata.py
Original file line number Diff line number Diff line change
Expand Up @@ -625,10 +625,11 @@ def _cast_to_stata_types(data: DataFrame) -> DataFrame:
if data[col].max() >= 2 ** 53 or data[col].min() <= -(2 ** 53):
ws = precision_loss_doc.format("int64", "float64")
elif dtype in (np.float32, np.float64):
value = data[col].max()
value = np.abs(data[col]).max()
if np.isinf(value):
raise ValueError(
f"Column {col} has a maximum value of infinity which is outside "
f"Column {col} has a maximum value of infinity "
"or a minimum value of -infinity which is outside "
"the range supported by Stata."
)
if dtype == np.float32 and value > float32_max:
Expand Down
30 changes: 26 additions & 4 deletions pandas/tests/io/test_stata.py
Original file line number Diff line number Diff line change
Expand Up @@ -1475,8 +1475,19 @@ def test_out_of_range_double(self):

df.loc[2, "ColumnTooBig"] = np.inf
msg = (
"Column ColumnTooBig has a maximum value of infinity which is outside "
"the range supported by Stata"
"Column ColumnTooBig has a maximum value of infinity "
"or a minimum value of -infinity which is outside "
"the range supported by Stata."
)
with pytest.raises(ValueError, match=msg):
with tm.ensure_clean() as path:
df.to_stata(path)

df.loc[2, "ColumnTooBig"] = -np.inf
msg = (
"Column ColumnTooBig has a maximum value of infinity "
"or a minimum value of -infinity which is outside "
"the range supported by Stata."
)
with pytest.raises(ValueError, match=msg):
with tm.ensure_clean() as path:
Expand Down Expand Up @@ -1509,8 +1520,19 @@ def test_out_of_range_float(self):

original.loc[2, "ColumnTooBig"] = np.inf
msg = (
"Column ColumnTooBig has a maximum value of infinity which "
"is outside the range supported by Stata"
"Column ColumnTooBig has a maximum value of infinity "
"or a minimum value of -infinity which is outside "
"the range supported by Stata."
)
with pytest.raises(ValueError, match=msg):
with tm.ensure_clean() as path:
original.to_stata(path)

original.loc[2, "ColumnTooBig"] = -np.inf
msg = (
"Column ColumnTooBig has a maximum value of infinity "
"or a minimum value of -infinity which is outside "
"the range supported by Stata."
)
with pytest.raises(ValueError, match=msg):
with tm.ensure_clean() as path:
Expand Down