Skip to content

Commit 3778325

Browse files
jbrockmendelproost
authored andcommitted
CLN: Exception catching in io (pandas-dev#28349)
* stop catching exception * CLN: catching Exception
1 parent 3bfb9db commit 3778325

File tree

5 files changed

+20
-31
lines changed

5 files changed

+20
-31
lines changed

pandas/core/indexes/accessors.py

+10-13
Original file line numberDiff line numberDiff line change
@@ -326,18 +326,15 @@ def __new__(cls, data):
326326
if orig is not None:
327327
data = Series(orig.values.categories, name=orig.name, copy=False)
328328

329-
try:
330-
if is_datetime64_dtype(data.dtype):
331-
return DatetimeProperties(data, orig)
332-
elif is_datetime64tz_dtype(data.dtype):
333-
return DatetimeProperties(data, orig)
334-
elif is_timedelta64_dtype(data.dtype):
335-
return TimedeltaProperties(data, orig)
336-
elif is_period_arraylike(data):
337-
return PeriodProperties(data, orig)
338-
elif is_datetime_arraylike(data):
339-
return DatetimeProperties(data, orig)
340-
except Exception:
341-
pass # we raise an attribute error anyway
329+
if is_datetime64_dtype(data.dtype):
330+
return DatetimeProperties(data, orig)
331+
elif is_datetime64tz_dtype(data.dtype):
332+
return DatetimeProperties(data, orig)
333+
elif is_timedelta64_dtype(data.dtype):
334+
return TimedeltaProperties(data, orig)
335+
elif is_period_arraylike(data):
336+
return PeriodProperties(data, orig)
337+
elif is_datetime_arraylike(data):
338+
return DatetimeProperties(data, orig)
342339

343340
raise AttributeError("Can only use .dt accessor with datetimelike values")

pandas/core/indexes/frozen.py

-5
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,7 @@ def difference(self, other):
7070
# TODO: Consider deprecating these in favor of `union` (xref gh-15506)
7171
__add__ = __iadd__ = union
7272

73-
# Python 2 compat
74-
def __getslice__(self, i, j):
75-
return self.__class__(super().__getslice__(i, j))
76-
7773
def __getitem__(self, n):
78-
# Python 3 compat
7974
if isinstance(n, slice):
8075
return self.__class__(super().__getitem__(n))
8176
return super().__getitem__(n)

pandas/io/common.py

+8-10
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,8 @@ def __next__(self):
9090

9191

9292
def _is_url(url) -> bool:
93-
"""Check to see if a URL has a valid protocol.
93+
"""
94+
Check to see if a URL has a valid protocol.
9495
9596
Parameters
9697
----------
@@ -101,10 +102,9 @@ def _is_url(url) -> bool:
101102
isurl : bool
102103
If `url` has a valid protocol return True otherwise False.
103104
"""
104-
try:
105-
return parse_url(url).scheme in _VALID_URLS
106-
except Exception:
105+
if not isinstance(url, str):
107106
return False
107+
return parse_url(url).scheme in _VALID_URLS
108108

109109

110110
def _expand_user(
@@ -171,18 +171,16 @@ def _stringify_path(
171171

172172
def is_s3_url(url) -> bool:
173173
"""Check for an s3, s3n, or s3a url"""
174-
try:
175-
return parse_url(url).scheme in ["s3", "s3n", "s3a"]
176-
except Exception:
174+
if not isinstance(url, str):
177175
return False
176+
return parse_url(url).scheme in ["s3", "s3n", "s3a"]
178177

179178

180179
def is_gcs_url(url) -> bool:
181180
"""Check for a gcs url"""
182-
try:
183-
return parse_url(url).scheme in ["gcs", "gs"]
184-
except Exception:
181+
if not isinstance(url, str):
185182
return False
183+
return parse_url(url).scheme in ["gcs", "gs"]
186184

187185

188186
def urlopen(*args, **kwargs):

pandas/io/parsers.py

-1
Original file line numberDiff line numberDiff line change
@@ -1064,7 +1064,6 @@ def _clean_options(self, options, engine):
10641064
)
10651065

10661066
if result.get(arg, depr_default) != depr_default:
1067-
# raise Exception(result.get(arg, depr_default), depr_default)
10681067
depr_warning += msg + "\n\n"
10691068
else:
10701069
result[arg] = parser_default

pandas/io/pickle.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -153,10 +153,10 @@ def read_pickle(path, compression="infer"):
153153
# We want to silence any warnings about, e.g. moved modules.
154154
warnings.simplefilter("ignore", Warning)
155155
return pickle.load(f)
156-
except Exception: # noqa: E722
156+
except Exception:
157157
try:
158158
return pc.load(f, encoding=None)
159-
except Exception: # noqa: E722
159+
except Exception:
160160
return pc.load(f, encoding="latin1")
161161
finally:
162162
f.close()

0 commit comments

Comments
 (0)