Skip to content
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

Fix deprecated Pandas syntax (up to v2.2.2) #164

Merged
merged 5 commits into from
Jun 4, 2024
Merged
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
8 changes: 4 additions & 4 deletions tableone/tableone.py
Original file line number Diff line number Diff line change
Expand Up @@ -889,8 +889,8 @@ def _create_cont_describe(self,
df_cont : pandas DataFrame
Summarise the continuous variables.
"""
aggfuncs = [pd.Series.count, np.mean, np.median, self._std,
self._q25, self._q75, min, max, self._t1_summary]
aggfuncs = ['count', 'mean', 'median', self._std,
self._q25, self._q75, 'min', 'max', self._t1_summary]

if self._dip_test:
aggfuncs.append(self._hartigan_dip)
Expand Down Expand Up @@ -1173,7 +1173,7 @@ def _create_smd_table(self, data: pd.DataFrame) -> pd.DataFrame:
n1=self.cont_describe['count'][p[0]].loc[v],
n2=self.cont_describe['count'][p[1]].loc[v],
unbiased=False)
df[colname.format(p[0], p[1])].loc[v] = smd
df.loc[v, colname.format(p[0], p[1])] = smd
except AttributeError:
pass

Expand All @@ -1187,7 +1187,7 @@ def _create_smd_table(self, data: pd.DataFrame) -> pd.DataFrame:
n1=self.cat_describe.loc[[v]]['freq'][p[0]].sum(),
n2=self.cat_describe.loc[[v]]['freq'][p[1]].sum(),
unbiased=False)
df[colname.format(p[0], p[1])].loc[v] = smd # type: ignore
df.loc[v, colname.format(p[0], p[1])] = smd # type: ignore
except AttributeError:
pass

Expand Down
12 changes: 8 additions & 4 deletions tests/unit/test_tableone.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,11 @@ def data_mixed(n=20):

mu, sigma = 50, 5
data_mixed['mixed numeric data'] = np.random.normal(mu, sigma, n)

# FutureWarning: Setting an item of incompatible dtype is deprecated
# and will raise an error in a future version of pandas.
data_mixed.loc[1, 'mixed numeric data'] = 'could not measure'

return data_mixed


Expand Down Expand Up @@ -483,15 +487,15 @@ def test_tableone_row_sort_pn(self, data_pn):

# a call to .index.levels[0] automatically sorts the levels
# instead, call values and use pd.unique as it preserves order
tableone_rows = pd.unique([x[0] for x in table.tableone.index.values])
tableone_rows = pd.Series([x[0] for x in table.tableone.index.values]).unique()

# default should not sort
for i, c in enumerate(columns):
# i+1 because we skip the first row, 'n'
assert tableone_rows[i+1] == c

table = TableOne(df, columns=columns, sort=True, label_suffix=False)
tableone_rows = pd.unique([x[0] for x in table.tableone.index.values])
tableone_rows = pd.Series([x[0] for x in table.tableone.index.values]).unique()
for i, c in enumerate(sorted(columns, key=lambda s: s.lower())):
# i+1 because we skip the first row, 'n'
assert tableone_rows[i+1] == c
Expand Down Expand Up @@ -967,7 +971,7 @@ def test_compute_standardized_mean_difference_continuous(self, data_pn):
'LOS': '0.121'}

for k in exp_smd:
smd = t.tableone.loc[k, 'Grouped by MechVent']['SMD (0,1)'][0]
smd = t.tableone.loc[k, 'Grouped by MechVent']['SMD (0,1)'].iloc[0]
assert smd == exp_smd[k]

def test_compute_standardized_mean_difference_categorical(self, data_pn):
Expand Down Expand Up @@ -995,7 +999,7 @@ def test_compute_standardized_mean_difference_categorical(self, data_pn):
'death': '0.017'}

for k in exp_smd:
smd = t.tableone.loc[k, 'Grouped by MechVent']['SMD (0,1)'][0]
smd = t.tableone.loc[k, 'Grouped by MechVent']['SMD (0,1)'].iloc[0]
assert smd == exp_smd[k]

def test_order_of_order_categorical_columns(self):
Expand Down
Loading