Skip to content

Commit 60ff4e1

Browse files
jbrockmendeljreback
authored andcommitted
CLN: catch Exception in fewer places, assorted cleanups (#28276)
1 parent bfff080 commit 60ff4e1

File tree

7 files changed

+18
-38
lines changed

7 files changed

+18
-38
lines changed

ci/code_checks.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ if [[ -z "$CHECK" || "$CHECK" == "code" ]]; then
203203
import sys
204204
import pandas
205205
206-
blacklist = {'bs4', 'gcsfs', 'html5lib', 'ipython', 'jinja2' 'hypothesis',
206+
blacklist = {'bs4', 'gcsfs', 'html5lib', 'ipython', 'jinja2', 'hypothesis',
207207
'lxml', 'numexpr', 'openpyxl', 'py', 'pytest', 's3fs', 'scipy',
208208
'tables', 'xlrd', 'xlsxwriter', 'xlwt'}
209209
mods = blacklist & set(m.split('.')[0] for m in sys.modules)

pandas/_libs/lib.pyx

+4-4
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ def fast_unique_multiple(list arrays, sort: bool=True):
235235
if sort is None:
236236
try:
237237
uniques.sort()
238-
except Exception:
238+
except TypeError:
239239
# TODO: RuntimeWarning?
240240
pass
241241

@@ -264,7 +264,7 @@ def fast_unique_multiple_list(lists: list, sort: bool=True) -> list:
264264
if sort:
265265
try:
266266
uniques.sort()
267-
except Exception:
267+
except TypeError:
268268
pass
269269

270270
return uniques
@@ -304,7 +304,7 @@ def fast_unique_multiple_list_gen(object gen, bint sort=True):
304304
if sort:
305305
try:
306306
uniques.sort()
307-
except Exception:
307+
except TypeError:
308308
pass
309309

310310
return uniques
@@ -1410,7 +1410,7 @@ def infer_datetimelike_array(arr: object) -> object:
14101410
try:
14111411
array_to_datetime(objs, errors='raise')
14121412
return 'datetime'
1413-
except:
1413+
except (ValueError, TypeError):
14141414
pass
14151415

14161416
# we are *not* going to infer from strings

pandas/core/common.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ def try_sort(iterable):
211211
listed = list(iterable)
212212
try:
213213
return sorted(listed)
214-
except Exception:
214+
except TypeError:
215215
return listed
216216

217217

pandas/core/groupby/grouper.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -583,9 +583,11 @@ def _get_grouper(
583583
# if the actual grouper should be obj[key]
584584
def is_in_axis(key):
585585
if not _is_label_like(key):
586+
items = obj._data.items
586587
try:
587-
obj._data.items.get_loc(key)
588-
except Exception:
588+
items.get_loc(key)
589+
except (KeyError, TypeError):
590+
# TypeError shows up here if we pass e.g. Int64Index
589591
return False
590592

591593
return True

pandas/core/groupby/ops.py

+5-25
Original file line numberDiff line numberDiff line change
@@ -615,14 +615,9 @@ def _aggregate(
615615
is_datetimelike,
616616
min_count=-1,
617617
):
618-
if values.ndim > 3:
618+
if values.ndim > 2:
619619
# punting for now
620-
raise NotImplementedError("number of dimensions is currently limited to 3")
621-
elif values.ndim > 2:
622-
for i, chunk in enumerate(values.transpose(2, 0, 1)):
623-
624-
chunk = chunk.squeeze()
625-
agg_func(result[:, :, i], counts, chunk, comp_ids, min_count)
620+
raise NotImplementedError("number of dimensions is currently limited to 2")
626621
else:
627622
agg_func(result, counts, values, comp_ids, min_count)
628623

@@ -640,20 +635,9 @@ def _transform(
640635
):
641636

642637
comp_ids, _, ngroups = self.group_info
643-
if values.ndim > 3:
638+
if values.ndim > 2:
644639
# punting for now
645-
raise NotImplementedError("number of dimensions is currently limited to 3")
646-
elif values.ndim > 2:
647-
for i, chunk in enumerate(values.transpose(2, 0, 1)):
648-
649-
transform_func(
650-
result[:, :, i],
651-
values,
652-
comp_ids,
653-
ngroups,
654-
is_datetimelike,
655-
**kwargs
656-
)
640+
raise NotImplementedError("number of dimensions is currently limited to 2")
657641
else:
658642
transform_func(result, values, comp_ids, ngroups, is_datetimelike, **kwargs)
659643

@@ -932,11 +916,7 @@ def _chop(self, sdata, slice_obj):
932916
class FrameSplitter(DataSplitter):
933917
def fast_apply(self, f, names):
934918
# must return keys::list, values::list, mutated::bool
935-
try:
936-
starts, ends = lib.generate_slices(self.slabels, self.ngroups)
937-
except Exception:
938-
# fails when all -1
939-
return [], True
919+
starts, ends = lib.generate_slices(self.slabels, self.ngroups)
940920

941921
sdata = self._get_sorted_data()
942922
return libreduction.apply_frame_axis0(sdata, f, names, starts, ends)

pandas/core/ops/__init__.py

+2-5
Original file line numberDiff line numberDiff line change
@@ -698,10 +698,7 @@ def na_op(x, y):
698698

699699
return result
700700

701-
def wrapper(self, other, axis=None):
702-
# Validate the axis parameter
703-
if axis is not None:
704-
self._get_axis_number(axis)
701+
def wrapper(self, other):
705702

706703
res_name = get_op_result_name(self, other)
707704
other = lib.item_from_zerodim(other)
@@ -1104,7 +1101,7 @@ def f(self, other):
11041101
# straight boolean comparisons we want to allow all columns
11051102
# (regardless of dtype to pass thru) See #4537 for discussion.
11061103
res = self._combine_const(other, func)
1107-
return res.fillna(True).astype(bool)
1104+
return res
11081105

11091106
f.__name__ = op_name
11101107

pandas/tests/test_downstream.py

+1
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ def _getitem_tuple(self, tup):
145145

146146
# Cython import warning
147147
@pytest.mark.filterwarnings("ignore:can't resolve:ImportWarning")
148+
@pytest.mark.filterwarnings("ignore:RangeIndex.* is deprecated:DeprecationWarning")
148149
def test_pyarrow(df):
149150

150151
pyarrow = import_module("pyarrow") # noqa

0 commit comments

Comments
 (0)