@@ -134,15 +134,16 @@ def maybe_downcast_to_dtype(result, dtype):
134
134
# a datetimelike
135
135
# GH12821, iNaT is casted to float
136
136
if dtype .kind in ["M" , "m" ] and result .dtype .kind in ["i" , "f" ]:
137
- try :
138
- result = result .astype (dtype )
139
- except Exception :
137
+ if hasattr (dtype , "tz" ):
138
+ # not a numpy dtype
140
139
if dtype .tz :
141
140
# convert to datetime and change timezone
142
141
from pandas import to_datetime
143
142
144
143
result = to_datetime (result ).tz_localize ("utc" )
145
144
result = result .tz_convert (dtype .tz )
145
+ else :
146
+ result = result .astype (dtype )
146
147
147
148
elif dtype .type is Period :
148
149
# TODO(DatetimeArray): merge with previous elif
@@ -214,14 +215,13 @@ def trans(x):
214
215
and notna (result ).all ()
215
216
):
216
217
new_result = trans (result ).astype (dtype )
217
- try :
218
- if np .allclose (new_result , result , rtol = 0 ):
219
- return new_result
220
- except Exception :
221
- # comparison of an object dtype with a number type could
222
- # hit here
218
+ if new_result .dtype .kind == "O" or result .dtype .kind == "O" :
219
+ # np.allclose may raise TypeError on object-dtype
223
220
if (new_result == result ).all ():
224
221
return new_result
222
+ else :
223
+ if np .allclose (new_result , result , rtol = 0 ):
224
+ return new_result
225
225
226
226
elif (
227
227
issubclass (dtype .type , np .floating )
@@ -286,14 +286,18 @@ def changeit():
286
286
# length of the boolean
287
287
try :
288
288
om = other [mask ]
289
+ except (IndexError , TypeError ):
290
+ # IndexError occurs in test_upcast when we have a boolean
291
+ # mask of the wrong shape
292
+ # TypeError occurs in test_upcast when `other` is a bool
293
+ pass
294
+ else :
289
295
om_at = om .astype (result .dtype )
290
296
if (om == om_at ).all ():
291
297
new_result = result .values .copy ()
292
298
new_result [mask ] = om_at
293
299
result [:] = new_result
294
300
return result , False
295
- except Exception :
296
- pass
297
301
298
302
# we are forced to change the dtype of the result as the input
299
303
# isn't compatible
@@ -324,7 +328,8 @@ def changeit():
324
328
325
329
try :
326
330
np .place (result , mask , other )
327
- except Exception :
331
+ except TypeError :
332
+ # e.g. int-dtype result and float-dtype other
328
333
return changeit ()
329
334
330
335
return result , False
@@ -636,24 +641,21 @@ def coerce_to_dtypes(result, dtypes):
636
641
raise AssertionError ("_coerce_to_dtypes requires equal len arrays" )
637
642
638
643
def conv (r , dtype ):
639
- try :
640
- if isna (r ):
641
- pass
642
- elif dtype == _NS_DTYPE :
643
- r = tslibs .Timestamp (r )
644
- elif dtype == _TD_DTYPE :
645
- r = tslibs .Timedelta (r )
646
- elif dtype == np .bool_ :
647
- # messy. non 0/1 integers do not get converted.
648
- if is_integer (r ) and r not in [0 , 1 ]:
649
- return int (r )
650
- r = bool (r )
651
- elif dtype .kind == "f" :
652
- r = float (r )
653
- elif dtype .kind == "i" :
654
- r = int (r )
655
- except Exception :
644
+ if np .any (isna (r )):
656
645
pass
646
+ elif dtype == _NS_DTYPE :
647
+ r = tslibs .Timestamp (r )
648
+ elif dtype == _TD_DTYPE :
649
+ r = tslibs .Timedelta (r )
650
+ elif dtype == np .bool_ :
651
+ # messy. non 0/1 integers do not get converted.
652
+ if is_integer (r ) and r not in [0 , 1 ]:
653
+ return int (r )
654
+ r = bool (r )
655
+ elif dtype .kind == "f" :
656
+ r = float (r )
657
+ elif dtype .kind == "i" :
658
+ r = int (r )
657
659
658
660
return r
659
661
@@ -794,13 +796,13 @@ def maybe_convert_objects(values: np.ndarray, convert_numeric: bool = True):
794
796
new_values = lib .maybe_convert_numeric (
795
797
values , set (), coerce_numeric = True
796
798
)
797
-
799
+ except Exception :
800
+ pass
801
+ else :
798
802
# if we are all nans then leave me alone
799
803
if not isna (new_values ).all ():
800
804
values = new_values
801
805
802
- except Exception :
803
- pass
804
806
else :
805
807
# soft-conversion
806
808
values = lib .maybe_convert_objects (values )
@@ -873,11 +875,12 @@ def soft_convert_objects(
873
875
if numeric and is_object_dtype (values .dtype ):
874
876
try :
875
877
converted = lib .maybe_convert_numeric (values , set (), coerce_numeric = True )
878
+ except Exception :
879
+ pass
880
+ else :
876
881
# If all NaNs, then do not-alter
877
882
values = converted if not isna (converted ).all () else values
878
883
values = values .copy () if copy else values
879
- except Exception :
880
- pass
881
884
882
885
return values
883
886
@@ -972,7 +975,7 @@ def try_timedelta(v):
972
975
973
976
try :
974
977
return to_timedelta (v )._ndarray_values .reshape (shape )
975
- except Exception :
978
+ except ValueError :
976
979
return v .reshape (shape )
977
980
978
981
inferred_type = lib .infer_datetimelike_array (ensure_object (v ))
0 commit comments