Skip to content

Commit d77d5e5

Browse files
authored
CoW: Avoid warnings in stata code (#56392)
1 parent bb14870 commit d77d5e5

File tree

3 files changed

+12
-27
lines changed

3 files changed

+12
-27
lines changed

pandas/io/stata.py

+9-14
Original file line numberDiff line numberDiff line change
@@ -342,10 +342,7 @@ def convert_delta_safe(base, deltas, unit) -> Series:
342342
has_bad_values = False
343343
if bad_locs.any():
344344
has_bad_values = True
345-
# reset cache to avoid SettingWithCopy checks (we own the DataFrame and the
346-
# `dates` Series is used to overwrite itself in the DataFramae)
347-
dates._reset_cacher()
348-
dates[bad_locs] = 1.0 # Replace with NaT
345+
dates._values[bad_locs] = 1.0 # Replace with NaT
349346
dates = dates.astype(np.int64)
350347

351348
if fmt.startswith(("%tc", "tc")): # Delta ms relative to base
@@ -462,11 +459,10 @@ def g(x: datetime) -> int:
462459
bad_loc = isna(dates)
463460
index = dates.index
464461
if bad_loc.any():
465-
dates = Series(dates)
466462
if lib.is_np_dtype(dates.dtype, "M"):
467-
dates[bad_loc] = to_datetime(stata_epoch)
463+
dates._values[bad_loc] = to_datetime(stata_epoch)
468464
else:
469-
dates[bad_loc] = stata_epoch
465+
dates._values[bad_loc] = stata_epoch
470466

471467
if fmt in ["%tc", "tc"]:
472468
d = parse_dates_safe(dates, delta=True)
@@ -596,9 +592,8 @@ def _cast_to_stata_types(data: DataFrame) -> DataFrame:
596592
for col in data:
597593
# Cast from unsupported types to supported types
598594
is_nullable_int = isinstance(data[col].dtype, (IntegerDtype, BooleanDtype))
599-
orig = data[col]
600595
# We need to find orig_missing before altering data below
601-
orig_missing = orig.isna()
596+
orig_missing = data[col].isna()
602597
if is_nullable_int:
603598
missing_loc = data[col].isna()
604599
if missing_loc.any():
@@ -1780,15 +1775,15 @@ def read(
17801775
for idx in valid_dtypes:
17811776
dtype = data.iloc[:, idx].dtype
17821777
if dtype not in (object_type, self._dtyplist[idx]):
1783-
data.iloc[:, idx] = data.iloc[:, idx].astype(dtype)
1778+
data.isetitem(idx, data.iloc[:, idx].astype(dtype))
17841779

17851780
data = self._do_convert_missing(data, convert_missing)
17861781

17871782
if convert_dates:
17881783
for i, fmt in enumerate(self._fmtlist):
17891784
if any(fmt.startswith(date_fmt) for date_fmt in _date_formats):
1790-
data.iloc[:, i] = _stata_elapsed_date_to_datetime_vec(
1791-
data.iloc[:, i], fmt
1785+
data.isetitem(
1786+
i, _stata_elapsed_date_to_datetime_vec(data.iloc[:, i], fmt)
17921787
)
17931788

17941789
if convert_categoricals and self._format_version > 108:
@@ -1863,7 +1858,7 @@ def _do_convert_missing(self, data: DataFrame, convert_missing: bool) -> DataFra
18631858
replacements[i] = replacement
18641859
if replacements:
18651860
for idx, value in replacements.items():
1866-
data.iloc[:, idx] = value
1861+
data.isetitem(idx, value)
18671862
return data
18681863

18691864
def _insert_strls(self, data: DataFrame) -> DataFrame:
@@ -1873,7 +1868,7 @@ def _insert_strls(self, data: DataFrame) -> DataFrame:
18731868
if typ != "Q":
18741869
continue
18751870
# Wrap v_o in a string to allow uint64 values as keys on 32bit OS
1876-
data.iloc[:, i] = [self.GSO[str(k)] for k in data.iloc[:, i]]
1871+
data.isetitem(i, [self.GSO[str(k)] for k in data.iloc[:, i]])
18771872
return data
18781873

18791874
def _do_select_columns(self, data: DataFrame, columns: Sequence[str]) -> DataFrame:

pandas/tests/io/test_common.py

-2
Original file line numberDiff line numberDiff line change
@@ -289,8 +289,6 @@ def test_read_expands_user_home_dir(
289289
):
290290
reader(path)
291291

292-
# TODO(CoW-warn) avoid warnings in the stata reader code
293-
@pytest.mark.filterwarnings("ignore:Setting a value on a view:FutureWarning")
294292
@pytest.mark.parametrize(
295293
"reader, module, path",
296294
[

pandas/tests/io/test_stata.py

+3-11
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,6 @@
3232
read_stata,
3333
)
3434

35-
# TODO(CoW-warn) avoid warnings in the stata reader code
36-
pytestmark = pytest.mark.filterwarnings(
37-
"ignore:Setting a value on a view:FutureWarning"
38-
)
39-
4035

4136
@pytest.fixture
4237
def mixed_frame():
@@ -140,7 +135,6 @@ def test_read_dta1(self, file, datapath):
140135

141136
tm.assert_frame_equal(parsed, expected)
142137

143-
@pytest.mark.filterwarnings("always")
144138
def test_read_dta2(self, datapath):
145139
expected = DataFrame.from_records(
146140
[
@@ -183,13 +177,11 @@ def test_read_dta2(self, datapath):
183177
path2 = datapath("io", "data", "stata", "stata2_115.dta")
184178
path3 = datapath("io", "data", "stata", "stata2_117.dta")
185179

186-
# TODO(CoW-warn) avoid warnings in the stata reader code
187-
# once fixed -> remove `raise_on_extra_warnings=False` again
188-
with tm.assert_produces_warning(UserWarning, raise_on_extra_warnings=False):
180+
with tm.assert_produces_warning(UserWarning):
189181
parsed_114 = self.read_dta(path1)
190-
with tm.assert_produces_warning(UserWarning, raise_on_extra_warnings=False):
182+
with tm.assert_produces_warning(UserWarning):
191183
parsed_115 = self.read_dta(path2)
192-
with tm.assert_produces_warning(UserWarning, raise_on_extra_warnings=False):
184+
with tm.assert_produces_warning(UserWarning):
193185
parsed_117 = self.read_dta(path3)
194186
# FIXME: don't leave commented-out
195187
# 113 is buggy due to limits of date format support in Stata

0 commit comments

Comments
 (0)