Skip to content

Commit 2a4e9f2

Browse files
committed
CI: troubleshoot segfault
1 parent 15539fa commit 2a4e9f2

File tree

9 files changed

+345
-444
lines changed

9 files changed

+345
-444
lines changed

pandas/core/arrays/datetimelike.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1005,9 +1005,19 @@ def fillna(self, value=None, method=None, limit=None):
10051005
else:
10061006
func = missing.backfill_1d
10071007

1008-
values = self.copy()
1008+
values = self._ndarray
1009+
if not is_period_dtype(self.dtype):
1010+
# For PeriodArray self._ndarray is i8, which gets copied
1011+
# by `func`. Otherwise we need to make a copy manually
1012+
# to avoid modifying `self` in-place.
1013+
values = values.copy()
1014+
10091015
new_values = func(values, limit=limit, mask=mask)
1010-
new_values = self._from_backing_data(new_values)
1016+
if is_datetime64tz_dtype(self.dtype):
1017+
# we need to pass int64 values to the constructor to avoid
1018+
# re-localizing incorrectly
1019+
new_values = new_values.view("i8")
1020+
new_values = type(self)(new_values, dtype=self.dtype)
10111021
else:
10121022
# fill with value
10131023
new_values = self.copy()

pandas/core/arrays/interval.py

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
is_datetime64_any_dtype,
2121
is_float_dtype,
2222
is_integer_dtype,
23+
is_interval,
2324
is_interval_dtype,
2425
is_list_like,
2526
is_object_dtype,
@@ -812,9 +813,7 @@ def take(self, indices, allow_fill=False, fill_value=None, axis=None, **kwargs):
812813

813814
fill_left = fill_right = fill_value
814815
if allow_fill:
815-
if (np.asarray(indices) == -1).any():
816-
# We have excel tests that pass fill_value=True, xref GH#36466
817-
fill_left, fill_right = self._validate_fill_value(fill_value)
816+
fill_left, fill_right = self._validate_fill_value(fill_value)
818817

819818
left_take = take(
820819
self.left, indices, allow_fill=allow_fill, fill_value=fill_left
@@ -825,33 +824,20 @@ def take(self, indices, allow_fill=False, fill_value=None, axis=None, **kwargs):
825824

826825
return self._shallow_copy(left_take, right_take)
827826

828-
def _validate_listlike(self, value):
829-
# list-like of intervals
830-
try:
831-
array = IntervalArray(value)
832-
# TODO: self._check_closed_matches(array, name="value")
833-
value_left, value_right = array.left, array.right
834-
except TypeError as err:
835-
# wrong type: not interval or NA
836-
msg = f"'value' should be an interval type, got {type(value)} instead."
837-
raise TypeError(msg) from err
838-
return value_left, value_right
839-
840-
def _validate_scalar(self, value):
841-
if isinstance(value, Interval):
842-
self._check_closed_matches(value, name="value")
843-
left, right = value.left, value.right
844-
elif is_valid_nat_for_dtype(value, self.left.dtype):
845-
# GH#18295
846-
left = right = value
847-
else:
848-
raise ValueError(
849-
"can only insert Interval objects and NA into an IntervalArray"
850-
)
851-
return left, right
852-
853827
def _validate_fill_value(self, value):
854-
return self._validate_scalar(value)
828+
if is_interval(value):
829+
self._check_closed_matches(value, name="fill_value")
830+
fill_left, fill_right = value.left, value.right
831+
elif not is_scalar(value) and notna(value):
832+
msg = (
833+
"'IntervalArray.fillna' only supports filling with a "
834+
"'scalar pandas.Interval or NA'. "
835+
f"Got a '{type(value).__name__}' instead."
836+
)
837+
raise ValueError(msg)
838+
else:
839+
fill_left = fill_right = self.left._na_value
840+
return fill_left, fill_right
855841

856842
def _validate_fillna_value(self, value):
857843
if not isinstance(value, Interval):
@@ -865,12 +851,26 @@ def _validate_fillna_value(self, value):
865851
return value.left, value.right
866852

867853
def _validate_insert_value(self, value):
868-
return self._validate_scalar(value)
854+
if isinstance(value, Interval):
855+
if value.closed != self.closed:
856+
raise ValueError(
857+
"inserted item must be closed on the same side as the index"
858+
)
859+
left_insert = value.left
860+
right_insert = value.right
861+
elif is_valid_nat_for_dtype(value, self.left.dtype):
862+
# GH#18295
863+
left_insert = right_insert = value
864+
else:
865+
raise ValueError(
866+
"can only insert Interval objects and NA into an IntervalIndex"
867+
)
868+
return left_insert, right_insert
869869

870870
def _validate_setitem_value(self, value):
871871
needs_float_conversion = False
872872

873-
if is_valid_nat_for_dtype(value, self.left.dtype):
873+
if is_scalar(value) and isna(value):
874874
# na value: need special casing to set directly on numpy arrays
875875
if is_integer_dtype(self.dtype.subtype):
876876
# can't set NaN on a numpy integer array

pandas/core/dtypes/common.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1215,10 +1215,6 @@ def needs_i8_conversion(arr_or_dtype) -> bool:
12151215
"""
12161216
if arr_or_dtype is None:
12171217
return False
1218-
if isinstance(arr_or_dtype, (np.dtype, ExtensionDtype)):
1219-
# fastpath
1220-
dtype = arr_or_dtype
1221-
return dtype.kind in ["m", "M"] or dtype.type is Period
12221218
return (
12231219
is_datetime_or_timedelta_dtype(arr_or_dtype)
12241220
or is_datetime64tz_dtype(arr_or_dtype)

pandas/core/missing.py

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,17 @@
77
import numpy as np
88

99
from pandas._libs import algos, lib
10-
from pandas._typing import DtypeObj
1110
from pandas.compat._optional import import_optional_dependency
1211

1312
from pandas.core.dtypes.cast import infer_dtype_from_array
1413
from pandas.core.dtypes.common import (
1514
ensure_float64,
15+
is_datetime64_dtype,
16+
is_datetime64tz_dtype,
1617
is_integer_dtype,
1718
is_numeric_v_string_like,
1819
is_scalar,
20+
is_timedelta64_dtype,
1921
needs_i8_conversion,
2022
)
2123
from pandas.core.dtypes.missing import isna
@@ -70,7 +72,7 @@ def mask_missing(arr, values_to_mask):
7072
return mask
7173

7274

73-
def clean_fill_method(method, allow_nearest: bool = False):
75+
def clean_fill_method(method, allow_nearest=False):
7476
# asfreq is compat for resampling
7577
if method in [None, "asfreq"]:
7678
return None
@@ -541,12 +543,7 @@ def _cubicspline_interpolate(xi, yi, x, axis=0, bc_type="not-a-knot", extrapolat
541543

542544

543545
def interpolate_2d(
544-
values,
545-
method="pad",
546-
axis=0,
547-
limit=None,
548-
fill_value=None,
549-
dtype: Optional[DtypeObj] = None,
546+
values, method="pad", axis=0, limit=None, fill_value=None, dtype=None
550547
):
551548
"""
552549
Perform an actual interpolation of values, values will be make 2-d if
@@ -587,14 +584,18 @@ def interpolate_2d(
587584
return values
588585

589586

590-
def _cast_values_for_fillna(values, dtype: DtypeObj):
587+
def _cast_values_for_fillna(values, dtype):
591588
"""
592589
Cast values to a dtype that algos.pad and algos.backfill can handle.
593590
"""
594591
# TODO: for int-dtypes we make a copy, but for everything else this
595592
# alters the values in-place. Is this intentional?
596593

597-
if needs_i8_conversion(dtype):
594+
if (
595+
is_datetime64_dtype(dtype)
596+
or is_datetime64tz_dtype(dtype)
597+
or is_timedelta64_dtype(dtype)
598+
):
598599
values = values.view(np.int64)
599600

600601
elif is_integer_dtype(values):
@@ -604,7 +605,7 @@ def _cast_values_for_fillna(values, dtype: DtypeObj):
604605
return values
605606

606607

607-
def _fillna_prep(values, mask=None, dtype: Optional[DtypeObj] = None):
608+
def _fillna_prep(values, mask=None, dtype=None):
608609
# boilerplate for pad_1d, backfill_1d, pad_2d, backfill_2d
609610
if dtype is None:
610611
dtype = values.dtype
@@ -619,19 +620,19 @@ def _fillna_prep(values, mask=None, dtype: Optional[DtypeObj] = None):
619620
return values, mask
620621

621622

622-
def pad_1d(values, limit=None, mask=None, dtype: Optional[DtypeObj] = None):
623+
def pad_1d(values, limit=None, mask=None, dtype=None):
623624
values, mask = _fillna_prep(values, mask, dtype)
624625
algos.pad_inplace(values, mask, limit=limit)
625626
return values
626627

627628

628-
def backfill_1d(values, limit=None, mask=None, dtype: Optional[DtypeObj] = None):
629+
def backfill_1d(values, limit=None, mask=None, dtype=None):
629630
values, mask = _fillna_prep(values, mask, dtype)
630631
algos.backfill_inplace(values, mask, limit=limit)
631632
return values
632633

633634

634-
def pad_2d(values, limit=None, mask=None, dtype: Optional[DtypeObj] = None):
635+
def pad_2d(values, limit=None, mask=None, dtype=None):
635636
values, mask = _fillna_prep(values, mask, dtype)
636637

637638
if np.all(values.shape):
@@ -642,7 +643,7 @@ def pad_2d(values, limit=None, mask=None, dtype: Optional[DtypeObj] = None):
642643
return values
643644

644645

645-
def backfill_2d(values, limit=None, mask=None, dtype: Optional[DtypeObj] = None):
646+
def backfill_2d(values, limit=None, mask=None, dtype=None):
646647
values, mask = _fillna_prep(values, mask, dtype)
647648

648649
if np.all(values.shape):

0 commit comments

Comments
 (0)