Skip to content

Commit

Permalink
Merge pull request #164 from tompollard/tp/bump_pandas_2_2_2
Browse files Browse the repository at this point in the history
Fix deprecated Pandas syntax (up to v2.2.2)
  • Loading branch information
tompollard authored Jun 4, 2024
2 parents fc20bc3 + 5c28200 commit 0ec30a2
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
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

0 comments on commit 0ec30a2

Please sign in to comment.