Skip to content
Merged
Changes from 1 commit
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
more review fixes
  • Loading branch information
jschendel committed Jul 10, 2018
commit bccb4f7f54f59a7e9d9db63218e03b45dd61cb1b
11 changes: 5 additions & 6 deletions pandas/core/arrays/interval.py
Original file line number Diff line number Diff line change
Expand Up @@ -454,11 +454,11 @@ def __getitem__(self, value):

def __setitem__(self, key, value):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this support the case where value is np.nan? Doesn't look like that currently works.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it currently fails. I'll see what can be done.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like there's also something strange going on with datetime data:

In [2]: ia = pd.interval_range(pd.Timestamp('20180101'), periods=3).values

In [3]: ia
Out[3]:
IntervalArray([(2018-01-01, 2018-01-02], (2018-01-02, 2018-01-03], (2018-01-03, 2018-01-04]],
              closed='right',
              dtype='interval[datetime64[ns]]')

In [4]: new_iv = pd.Interval(pd.Timestamp('20180101'), pd.Timestamp('20180105'))

In [5]: new_iv
Out[5]: Interval('2018-01-01', '2018-01-05', closed='right')

In [6]: ia[0] = new_iv

In [7]: ia
Out[7]: ---------------------------------------------------------------------------
ValueError: left side of interval must be <= right side

In [8]: ia.left
Out[8]: DatetimeIndex(['2018-01-01', '2018-01-05', '2018-01-03'], dtype='datetime64[ns]', freq='D')

In [9]: ia.right
Out[9]: DatetimeIndex(['2018-01-05', '2018-01-03', '2018-01-04'], dtype='datetime64[ns]', freq='D')

# na value: need special casing to set directly on numpy arrays
_needs_float_conversion = False
needs_float_conversion = False
if is_scalar(value) and isna(value):
if is_integer_dtype(self.dtype.subtype):
# can't set NaN on a numpy integer array
_needs_float_conversion = True
needs_float_conversion = True
elif is_datetime64_any_dtype(self.dtype.subtype):
# need proper NaT to set directly on the numpy array
value = np.datetime64('NaT')
Expand All @@ -485,13 +485,13 @@ def __setitem__(self, key, value):
# Need to ensure that left and right are updated atomically, so we're
# forced to copy, update the copy, and swap in the new values.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure I am clear why this is problematic. is there a failure possible when updating the 2nd one?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without the forcing the copy I'm seeing test_setitem_sequence fail:

def test_setitem_sequence(self, data):
arr = pd.Series(data)
original = data.copy()
arr[[0, 1]] = [data[1], data[0]]
assert arr[0] == original[1]
assert arr[1] == original[0]

left = self.left.copy(deep=True)
if _needs_float_conversion:
if needs_float_conversion:
left = left.astype('float')
left.values[key] = value_left
self._left = left

right = self.right.copy(deep=True)
if _needs_float_conversion:
if needs_float_conversion:
right = right.astype('float')
right.values[key] = value_right
self._right = right
Expand All @@ -505,7 +505,7 @@ def fillna(self, value=None, method=None, limit=None):

if not isinstance(value, ABCInterval):
msg = ("'IntervalArray.fillna' only supports filling with a "
"'scalar pandas.Interval'. Got a '{}' instead."
"scalar 'pandas.Interval'. Got a '{}' instead."
.format(type(value).__name__))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Single quote is in the wrong place. Should be 'pandas.Interval'.

raise TypeError(msg)

Expand Down Expand Up @@ -580,7 +580,6 @@ def _concat_same_type(cls, to_concat):
raise ValueError("Intervals must all be closed on the same side.")
closed = closed.pop()

# TODO: avoid intermediate list
left = np.concatenate([interval.left for interval in to_concat])
right = np.concatenate([interval.right for interval in to_concat])
return cls._simple_new(left, right, closed=closed, copy=False)
Expand Down