Skip to content
This repository was archived by the owner on Feb 2, 2024. It is now read-only.

Commit d917971

Browse files
authored
Add index to df.drop() (#627)
* Add index to df.drop() * Skip test
1 parent 632b554 commit d917971

File tree

2 files changed

+97
-27
lines changed

2 files changed

+97
-27
lines changed

sdc/datatypes/hpat_pandas_dataframe_functions.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -925,8 +925,8 @@ def sdc_pandas_dataframe_drop_impl(df, labels=None, axis=0, index=None, columns=
925925
column_list.append((f'new_col_{column}_data_df', column))
926926

927927
data = ', '.join(f'"{column_name}": {column}' for column, column_name in column_list)
928-
# TODO: Handle index
929-
func_text.append(f"return pandas.DataFrame({{{data}}})\n")
928+
index = 'df.index'
929+
func_text.append(f"return pandas.DataFrame({{{data}}}, index={index})\n")
930930
func_definition.extend([indent + func_line for func_line in func_text])
931931
func_def = '\n'.join(func_definition)
932932

@@ -977,7 +977,7 @@ def sdc_pandas_dataframe_drop(df, labels=None, axis=0, index=None, columns=None,
977977
*************************************************
978978
Pandas DataFrame method :meth:`pandas.DataFrame.drop` implementation.
979979
.. only:: developer
980-
Test: python -m sdc.runtests -k sdc.tests.test_dataframe.TestDataFrame.test_drop*
980+
Test: python -m sdc.runtests -k sdc.tests.test_dataframe.TestDataFrame.test_df_drop*
981981
Parameters
982982
-----------
983983
df: :obj:`pandas.DataFrame`

sdc/tests/test_dataframe.py

Lines changed: 94 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1148,42 +1148,112 @@ def test_impl(df):
11481148
h_out = hpat_func(df)
11491149
pd.testing.assert_frame_equal(out, h_out)
11501150

1151-
def test_df_drop_one_column(self):
1151+
def test_df_drop_one_column_unboxing(self):
11521152
def test_impl(df):
11531153
return df.drop(columns='A')
11541154

1155-
df = pd.DataFrame({'A': [1.0, 2.0, np.nan, 1.0], 'B': [4, 5, 6, 7], 'C': [1.0, 2.0, np.nan, 1.0]})
1156-
hpat_func = self.jit(test_impl)
1157-
pd.testing.assert_frame_equal(hpat_func(df), test_impl(df))
1155+
index_to_test = [[1, 2, 3, 4],
1156+
[.1, .2, .3, .4],
1157+
None,
1158+
['a', 'b', 'c', 'd']]
1159+
1160+
sdc_func = self.jit(test_impl)
1161+
1162+
for index in index_to_test:
1163+
with self.subTest(index=index):
1164+
df = pd.DataFrame({'A': [1.0, 2.0, np.nan, 1.0], 'B': [4, 5, 6, 7], 'C': [1.0, 2.0, np.nan, 1.0]},
1165+
index=index)
1166+
pd.testing.assert_frame_equal(sdc_func(df), test_impl(df))
1167+
1168+
def test_df_drop_one_column(self):
1169+
def test_impl(index):
1170+
df = pd.DataFrame({'A': [1.0, 2.0, np.nan, 1.0], 'B': [4, 5, 6, 7], 'C': [1.0, 2.0, np.nan, 1.0]},
1171+
index=index)
1172+
return df.drop(columns='A')
1173+
1174+
index_to_test = [[1, 2, 3, 4],
1175+
[.1, .2, .3, .4],
1176+
['a', 'b', 'c', 'd']]
1177+
1178+
sdc_func = self.jit(test_impl)
1179+
1180+
for index in index_to_test:
1181+
with self.subTest(index=index):
1182+
pd.testing.assert_frame_equal(sdc_func(index), test_impl(index))
1183+
1184+
def test_df_drop_tuple_column_unboxing(self):
1185+
def gen_test_impl(do_jit=False):
1186+
def test_impl(df):
1187+
if do_jit == True: # noqa
1188+
return df.drop(columns=('A', 'C'))
1189+
else:
1190+
return df.drop(columns=['A', 'C'])
1191+
1192+
return test_impl
1193+
1194+
index_to_test = [[1, 2, 3, 4],
1195+
[.1, .2, .3, .4],
1196+
None,
1197+
['a', 'b', 'c', 'd']]
1198+
1199+
test_impl = gen_test_impl()
1200+
sdc_func = self.jit(gen_test_impl(do_jit=True))
1201+
1202+
for index in index_to_test:
1203+
with self.subTest(index=index):
1204+
df = pd.DataFrame({'A': [1.0, 2.0, np.nan, 1.0], 'B': [4, 5, 6, 7], 'C': [1.0, 2.0, np.nan, 1.0]},
1205+
index=index)
1206+
pd.testing.assert_frame_equal(sdc_func(df), test_impl(df))
11581207

1159-
@skip_sdc_jit
11601208
def test_df_drop_tuple_column(self):
1161-
# Pandas supports only list as a parameter
1162-
def test_impl(df):
1163-
return df.drop(columns=['A', 'B'])
1209+
def gen_test_impl(do_jit=False):
1210+
def test_impl(index):
1211+
df = pd.DataFrame({'A': [1.0, 2.0, np.nan, 1.0], 'B': [4, 5, 6, 7], 'C': [1.0, 2.0, np.nan, 1.0]},
1212+
index=index)
1213+
if do_jit == True: # noqa
1214+
return df.drop(columns=('A', 'C'))
1215+
else:
1216+
return df.drop(columns=['A', 'C'])
11641217

1165-
# Numba supports only tuple iteration
1166-
def test_sdc_impl(df):
1167-
return df.drop(columns=('A', 'B'))
1218+
return test_impl
11681219

1169-
df = pd.DataFrame({'A': [1.0, 2.0, np.nan, 1.0], 'B': [4, 5, 6, 7], 'C': [1.0, 2.0, np.nan, 1.0]})
1170-
hpat_func = self.jit(test_sdc_impl)
1171-
pd.testing.assert_frame_equal(hpat_func(df), test_impl(df))
1220+
index_to_test = [[1, 2, 3, 4],
1221+
[.1, .2, .3, .4],
1222+
['a', 'b', 'c', 'd']]
1223+
1224+
test_impl = gen_test_impl()
1225+
sdc_func = self.jit(gen_test_impl(do_jit=True))
11721226

1173-
@unittest.skip("Implement Index for DataFrames")
1227+
for index in index_to_test:
1228+
with self.subTest(index=index):
1229+
pd.testing.assert_frame_equal(sdc_func(index), test_impl(index))
1230+
1231+
@unittest.skip("ValueError when return empty dataframe")
11741232
def test_df_drop_tuple_columns_all(self):
1175-
def test_impl(df):
1176-
return df.drop(columns=['A', 'B', 'C'])
1233+
def gen_test_impl(do_jit=False):
1234+
def test_impl(df):
1235+
if do_jit == True: # noqa
1236+
return df.drop(columns=('A', 'B', 'C'))
1237+
else:
1238+
return df.drop(columns=['A', 'B', 'C'])
11771239

1178-
# Numba supports only tuple iteration
1179-
def test_sdc_impl(df):
1180-
return df.drop(columns=('A', 'B', 'C'))
1240+
return test_impl
11811241

1182-
df = pd.DataFrame({'A': [1.0, 2.0, np.nan, 1.0], 'B': [4, 5, 6, 7], 'C': [1.0, 2.0, np.nan, 1.0]})
1183-
hpat_func = self.jit(test_sdc_impl)
1184-
pd.testing.assert_frame_equal(hpat_func(df), test_impl(df))
1242+
index_to_test = [[1, 2, 3, 4],
1243+
[.1, .2, .3, .4],
1244+
None,
1245+
['a', 'b', 'c', 'd']]
1246+
1247+
test_impl = gen_test_impl()
1248+
sdc_func = self.jit(gen_test_impl(do_jit=True))
1249+
1250+
for index in index_to_test:
1251+
with self.subTest(index=index):
1252+
df = pd.DataFrame({'A': [1.0, 2.0, np.nan, 1.0], 'B': [4, 5, 6, 7], 'C': [1.0, 2.0, np.nan, 1.0]},
1253+
index=index)
1254+
1255+
pd.testing.assert_frame_equal(sdc_func(df), test_impl(df))
11851256

1186-
@skip_sdc_jit
11871257
def test_df_drop_by_column_errors_ignore(self):
11881258
def test_impl(df):
11891259
return df.drop(columns='M', errors='ignore')

0 commit comments

Comments
 (0)