Skip to content

Commit 306e4b2

Browse files
authored
Merge pull request #241 from frank1010111/pandas-deprecation
Fix Pandas deprecations
2 parents 3e9ea97 + 5477513 commit 306e4b2

File tree

4 files changed

+31
-9
lines changed

4 files changed

+31
-9
lines changed
2.73 KB
Loading

tests/test_well.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import os
66
from pathlib import Path
77

8+
import pandas as pd
9+
810
import welly
911
from welly import Well
1012

@@ -148,3 +150,11 @@ def test_iter_well(well):
148150
for curve in well:
149151
assert curve == well.data['CALI']
150152
break
153+
154+
def test_df_object_cols(df):
155+
156+
df["Test object"] = "54"
157+
df["test_str"] = '1z'
158+
well = Well.from_df(df)
159+
assert all(well.df()["Test object"] == 54)
160+
assert all(well.df()["test_str"] == "1z")

welly/curve.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import numpy as np
1111
import pandas as pd
1212
from pandas.api.types import is_any_real_numeric_dtype
13+
from pandas.api.types import is_numeric_dtype
1314
from scipy.interpolate import interp1d
1415

1516
from welly.plot import plot_2d_curve, plot_curve, plot_kde_curve
@@ -970,7 +971,7 @@ def to_basis(self,
970971
Curve. The current instance in the new basis.
971972
"""
972973
# category data type or a string in data defaults to 'nearest'
973-
if pd.api.types.is_categorical_dtype(self.df.iloc[:, 0]) or pd.api.types.is_string_dtype(self.df.iloc[:, 0]):
974+
if not is_numeric_dtype(self.df.iloc[:, 0]):
974975
interp_kind = 'nearest'
975976

976977
new_curve = copy.deepcopy(self)

welly/well.py

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -632,17 +632,28 @@ def df(self,
632632
# swap MultiIndex levels
633633
df = df.swaplevel()
634634

635-
# I think this is the wrong place to do this.
636-
# Anyway, use i not name just in case there are duplicate names.
637-
for i, (_, column) in enumerate(df.iteritems()):
638-
if is_object_dtype(column.dtype):
639-
try:
640-
df.iloc[:, i] = column.astype(float)
641-
except ValueError:
642-
pass
635+
df = self._convert_object_cols_to_numeric(df)
643636

644637
return df
645638

639+
def _convert_object_cols_to_numeric(self, df):
640+
"""
641+
Convert object columns into numeric columns, if possible.
642+
643+
Args:
644+
df (pd.DataFrame): dataframe to work
645+
Returns:
646+
pd.DataFrame. Whole dataframe with conversions
647+
"""
648+
df_nonobject = df.select_dtypes(exclude="object")
649+
df_object = df.select_dtypes(include="object")
650+
for col in df_object.columns:
651+
try:
652+
df_object[col] = pd.to_numeric(df_object[col])
653+
except ValueError:
654+
pass
655+
return pd.concat([df_nonobject, df_object], axis=1)
656+
646657
def add_curves_from_las(self, fname, remap=None, funcs=None):
647658
"""
648659
Given a LAS file, add curves from it to the current well instance.

0 commit comments

Comments
 (0)