Skip to content

Commit 64553b8

Browse files
committed
TST: Resolve pandas warnings emitted during tests
This resolves a few warnings related to accessing things. Removing the `inplace=True` qualifier seems undesirable despite the warning being emitted for it, but it turns out that generally when this is given pandas is creating a copy internally anyway. pandas seem to have a general desire to remove the `inplace=` options everywhere so that code is not mutating, but remains more immutable. See the following comment from years ago. pandas-dev/pandas#16529 (comment)
1 parent 2cfc25e commit 64553b8

File tree

7 files changed

+53
-35
lines changed

7 files changed

+53
-35
lines changed

src/xtgeo/grid3d/_grid_etc1.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -424,17 +424,17 @@ def get_ijk_from_points(
424424
proplist[columnnames[2]] = karr
425425

426426
mydataframe = pd.DataFrame.from_dict(proplist)
427-
mydataframe.replace(UNDEF_INT, -1, inplace=True)
427+
mydataframe = mydataframe.replace(UNDEF_INT, -1)
428428

429429
if fmt == "float":
430430
mydataframe[columnnames[0]] = mydataframe[columnnames[0]].astype("float")
431431
mydataframe[columnnames[1]] = mydataframe[columnnames[1]].astype("float")
432432
mydataframe[columnnames[2]] = mydataframe[columnnames[2]].astype("float")
433433

434434
if undef != -1:
435-
mydataframe[columnnames[0]].replace(-1, undef, inplace=True)
436-
mydataframe[columnnames[1]].replace(-1, undef, inplace=True)
437-
mydataframe[columnnames[2]].replace(-1, undef, inplace=True)
435+
mydataframe[columnnames[0]] = mydataframe[columnnames[0]].replace(-1, undef)
436+
mydataframe[columnnames[1]] = mydataframe[columnnames[1]].replace(-1, undef)
437+
mydataframe[columnnames[2]] = mydataframe[columnnames[2]].replace(-1, undef)
438438

439439
if dataframe:
440440
return mydataframe

src/xtgeo/grid3d/_grid_wellzone.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,19 +102,19 @@ def report_zone_mismatch(
102102

103103
for zname in (zonelogname, zmodel):
104104
if skiprange: # needed check; du to a bug in pandas version 0.21 .. 0.23
105-
df[zname].replace(skiprange, -888, inplace=True)
106-
df[zname].fillna(-999, inplace=True)
105+
df[zname] = df[zname].replace(skiprange, -888)
106+
df[zname] = df[zname].fillna(-999)
107107
if perflogname:
108108
if perflogname in df.columns:
109-
df[perflogname].replace(np.nan, -1, inplace=True)
109+
df[perflogname] = df[perflogname].replace(np.nan, -1)
110110
pfr1, pfr2 = perflogrange
111111
df[zname] = np.where(df[perflogname] < pfr1, -899, df[zname])
112112
df[zname] = np.where(df[perflogname] > pfr2, -899, df[zname])
113113
else:
114114
return None
115115
if filterlogname:
116116
if filterlogname in df.columns:
117-
df[filterlogname].replace(np.nan, -1, inplace=True)
117+
df[filterlogname] = df[filterlogname].replace(np.nan, -1)
118118
ffr1, ffr2 = filterlogrange
119119
df[zname] = np.where(df[filterlogname] < ffr1, -919, df[zname])
120120
df[zname] = np.where(df[filterlogname] > ffr2, -919, df[zname])

src/xtgeo/well/_well_io.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ def import_rms_ascii(
109109

110110
dfr = pd.read_csv(
111111
wfile.file,
112-
delim_whitespace=True,
112+
sep=r"\s+",
113113
skiprows=lnum,
114114
header=None,
115115
names=xlognames_all,
@@ -220,8 +220,7 @@ def export_rms_ascii(self, wfile, precision=4):
220220
print(f"{lname} {self.get_logtype(lname)} {usewrec}", file=fwell)
221221

222222
# now export all logs as pandas framework
223-
tmpdf = self._wdata.data.copy()
224-
tmpdf.fillna(value=-999, inplace=True)
223+
tmpdf = self._wdata.data.copy().fillna(value=-999)
225224

226225
# make the disc as is np.int
227226
for lname in self.wlogtypes:

src/xtgeo/xyz/_xyz_data.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -300,15 +300,14 @@ def _ensure_consistency_df_dtypes(self):
300300
for name, attr_type in self._attr_types.items():
301301
if attr_type == _AttrType.CONT.value:
302302
logger.debug("Replacing CONT undef...")
303-
self._df[name].replace(
303+
self._df.loc[:, name] = self._df[name].replace(
304304
self._undef_cont,
305305
np.float64(UNDEF_CONT).astype(self._floatbits),
306-
inplace=True,
307306
)
308307
else:
309308
logger.debug("Replacing INT undef...")
310-
self._df[name].replace(
311-
self._undef_disc, np.int32(UNDEF_DISC), inplace=True
309+
self._df.loc[:, name] = self._df[name].replace(
310+
self._undef_disc, np.int32(UNDEF_DISC)
312311
)
313312
logger.info("Processed dataframe: %s", list(self._df.dtypes))
314313

@@ -579,7 +578,7 @@ def create_relative_hlen(self):
579578
distance.append(math.hypot((previous_x - x), (y - previous_y)))
580579
previous_x, previous_y = x, y
581580

582-
self._df[_AttrName.R_HLEN_NAME.value] = pd.Series(
581+
self._df.loc[:, _AttrName.R_HLEN_NAME.value] = pd.Series(
583582
np.cumsum(distance), index=self._df.index
584583
)
585584
self.ensure_consistency()

src/xtgeo/xyz/_xyz_io.py

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
"""Private import and export routines for XYZ stuff."""
22

3+
import contextlib
4+
35
import numpy as np
46
import pandas as pd
57

@@ -19,7 +21,7 @@ def import_xyz(pfile, zname="Z_TVDSS"):
1921
"yname": "Y_UTMN",
2022
"values": pd.read_csv(
2123
pfile.file,
22-
delim_whitespace=True,
24+
sep=r"\s+",
2325
skiprows=0,
2426
header=None,
2527
names=["X_UTME", "Y_UTMN", zname],
@@ -70,7 +72,7 @@ def import_zmap(pfile, zname="Z_TVDSS"):
7072

7173
df = pd.read_csv(
7274
pfile.file,
73-
delim_whitespace=True,
75+
sep=r"\s+",
7476
skiprows=16,
7577
header=None,
7678
names=[xname, yname, zname, pname],
@@ -150,20 +152,24 @@ def import_rms_attr(pfile, zname="Z_TVDSS"):
150152

151153
dfr = pd.read_csv(
152154
pfile.file,
153-
delim_whitespace=True,
155+
sep=r"\s+",
154156
skiprows=skiprows,
155157
header=None,
156158
names=names,
157159
dtype=dtypes,
158160
)
159161
for col in dfr.columns[3:]:
160162
if col in _attrs:
163+
# pandas gives a FutureWarning here due to casting what was
164+
# previously a string to a float/int.
161165
if _attrs[col] == "float":
162-
dfr[col].replace("UNDEF", UNDEF, inplace=True)
166+
dfr[col] = dfr[col].replace("UNDEF", UNDEF).astype(float)
163167
elif _attrs[col] == "int":
164-
dfr[col].replace("UNDEF", UNDEF_INT, inplace=True)
165-
# cast to numerical if possible
166-
dfr[col] = pd.to_numeric(dfr[col], errors="ignore")
168+
dfr[col] = dfr[col].replace("UNDEF", UNDEF_INT).astype(int)
169+
170+
# cast to numerical if possible
171+
with contextlib.suppress(ValueError, TypeError):
172+
dfr[col] = pd.to_numeric(dfr[col])
167173

168174
kwargs["values"] = dfr
169175
kwargs["attributes"] = _attrs
@@ -314,9 +320,9 @@ def export_rms_attr(self, pfile, attributes=True, pfilter=None, ispolygons=False
314320
if col in df.columns:
315321
fout.write(transl[self._attrs[col]] + " " + col + "\n")
316322
if self._attrs[col] == "int":
317-
df[col].replace(UNDEF_INT, "UNDEF", inplace=True)
323+
df[col] = df[col].replace(UNDEF_INT, "UNDEF")
318324
elif self._attrs[col] == "float":
319-
df[col].replace(UNDEF, "UNDEF", inplace=True)
325+
df[col] = df[col].replace(UNDEF, "UNDEF")
320326

321327
with open(pfile, mode) as fc:
322328
df.to_csv(fc, sep=" ", header=None, columns=columns, index=False)

src/xtgeo/xyz/_xyz_oper.py

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -147,18 +147,18 @@ def operation_polygons_v2(self, poly, value, opname="add", inside=True, where=Tr
147147
dataframe = self.get_dataframe()
148148

149149
if opname == "add":
150-
dataframe[self.zname][tmpdf._TMP == 1] += value
150+
dataframe.loc[tmpdf._TMP == 1, self.zname] += value
151151
elif opname == "sub":
152-
dataframe[self.zname][tmpdf._TMP == 1] -= value
152+
dataframe.loc[tmpdf._TMP == 1, self.zname] -= value
153153
elif opname == "mul":
154-
dataframe[self.zname][tmpdf._TMP == 1] *= value
154+
dataframe.loc[tmpdf._TMP == 1, self.zname] *= value
155155
elif opname == "div":
156156
if value != 0.0:
157-
dataframe[self.zname][tmpdf._TMP == 1] /= value
157+
dataframe.loc[tmpdf._TMP == 1, self.zname] /= value
158158
else:
159-
dataframe[self.zname][tmpdf._TMP == 1] = 0.0
159+
dataframe.loc[tmpdf._TMP == 1, self.zname] = 0.0
160160
elif opname == "set":
161-
dataframe[self.zname][tmpdf._TMP == 1] = value
161+
dataframe.loc[tmpdf._TMP == 1, self.zname] = value
162162
elif opname == "eli":
163163
dataframe = dataframe[tmpdf._TMP == 0]
164164
dataframe.reset_index(inplace=True, drop=True)
@@ -561,7 +561,14 @@ def extend(self, distance, nsamples, addhlen=True):
561561

562562
# setting row0[2] as row1[2] is intentional, as this shall be a 2D lenght!
563563
ier, newx, newy, _ = _cxtgeo.x_vector_linint2(
564-
row1[0], row1[1], row1[2], row0[0], row0[1], row1[2], distance, 12
564+
row1.iloc[0],
565+
row1.iloc[1],
566+
row1.iloc[2],
567+
row0.iloc[0],
568+
row0.iloc[1],
569+
row1.iloc[2],
570+
distance,
571+
12,
565572
)
566573

567574
if ier != 0:
@@ -582,7 +589,14 @@ def extend(self, distance, nsamples, addhlen=True):
582589

583590
# setting row1[2] as row0[2] is intentional, as this shall be a 2D lenght!
584591
ier, newx, newy, _ = _cxtgeo.x_vector_linint2(
585-
row0[0], row0[1], row0[2], row1[0], row1[1], row0[2], distance, 11
592+
row0.iloc[0],
593+
row0.iloc[1],
594+
row0.iloc[2],
595+
row1.iloc[0],
596+
row1.iloc[1],
597+
row0.iloc[2],
598+
distance,
599+
11,
586600
)
587601

588602
rown[self.xname] = newx

tests/test_well/test_well.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -872,8 +872,8 @@ def test_create_surf_distance_log_more(tmp_path, loadwell1, testdata_path):
872872

873873
for zname in (zonelogname, zmodel):
874874
if skiprange: # needed check; du to a bug in pandas version 0.21 .. 0.23
875-
dfr[zname].replace(skiprange, -888, inplace=True)
876-
dfr[zname].fillna(-999, inplace=True)
875+
dfr[zname] = dfr[zname].replace(skiprange, -888)
876+
dfr[zname] = dfr[zname].fillna(-999)
877877
# now there are various variotions on how to count mismatch:
878878
# dfuse 1: count matches when zonelogname is valid (exclude -888)
879879
# dfuse 2: count matches when zonelogname OR zmodel are valid (exclude < -888

0 commit comments

Comments
 (0)