Skip to content

BUG: Fix IntervalIndex.insert to allow inserting NaN #18300

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Nov 25, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
chucknull -> is_scalar + isna
  • Loading branch information
jschendel committed Nov 25, 2017
commit a951f279c43d11d18e3ce2e836e49dcee91c2409
2 changes: 1 addition & 1 deletion pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -3734,7 +3734,7 @@ def insert(self, loc, item):
-------
new_index : Index
"""
if lib.checknull(item):
if is_scalar(item) and isna(item):
# GH 18295
item = self._na_value

Expand Down
6 changes: 3 additions & 3 deletions pandas/core/indexes/category.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import numpy as np
from pandas._libs import index as libindex, lib
from pandas._libs import index as libindex

from pandas import compat
from pandas.compat.numpy import function as nv
Expand All @@ -12,7 +12,7 @@
is_scalar)
from pandas.core.common import (_asarray_tuplesafe,
_values_from_object)
from pandas.core.dtypes.missing import array_equivalent
from pandas.core.dtypes.missing import array_equivalent, isna
from pandas.core.algorithms import take_1d


Expand Down Expand Up @@ -690,7 +690,7 @@ def insert(self, loc, item):

"""
code = self.categories.get_indexer([item])
if (code == -1) and not lib.checknull(item):
if (code == -1) and not (is_scalar(item) and isna(item)):
raise TypeError("cannot insert an item into a CategoricalIndex "
"that is not already an existing category")

Expand Down
2 changes: 1 addition & 1 deletion pandas/core/indexes/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -1757,7 +1757,7 @@ def insert(self, loc, item):
-------
new_index : Index
"""
if lib.checknull(item):
if is_scalar(item) and isna(item):
# GH 18295
item = self._na_value

Expand Down
4 changes: 2 additions & 2 deletions pandas/core/indexes/interval.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
Index, _ensure_index,
default_pprint, _index_shared_docs)

from pandas._libs import lib, Timestamp, Timedelta
from pandas._libs import Timestamp, Timedelta
from pandas._libs.interval import (
Interval, IntervalMixin, IntervalTree,
intervals_to_interval_bounds)
Expand Down Expand Up @@ -1007,7 +1007,7 @@ def insert(self, loc, item):
'side as the index')
left_insert = item.left
right_insert = item.right
elif lib.checknull(item):
elif is_scalar(item) and isna(item):
# GH 18295
left_insert = right_insert = item
Copy link
Contributor

Choose a reason for hiding this comment

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

yes u need to use a nan compat with left iow this could be a NaT

Copy link
Contributor

Choose a reason for hiding this comment

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

i think we have a left.na_value iirc (might be spelled differently)

Copy link
Contributor

Choose a reason for hiding this comment

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

or maybe the underlying already handles this in the insert

Copy link
Member Author

Choose a reason for hiding this comment

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

Updated. Procedure I'm following is "check if any type of NA value is passed -> raise if the wrong type of NA is passed". I suppose I could just bypass this and only check if the right type of NA is passed, if that would be preferred.

else:
Expand Down
4 changes: 2 additions & 2 deletions pandas/core/indexes/timedeltas.py
Original file line number Diff line number Diff line change
Expand Up @@ -858,12 +858,12 @@ def insert(self, loc, item):
item = Timedelta(item)
except Exception:
pass
elif lib.checknull(item):
elif is_scalar(item) and isna(item):
# GH 18295
item = self._na_value

freq = None
if isinstance(item, Timedelta) or (item is self._na_value):
if isinstance(item, Timedelta) or (is_scalar(item) and isna(item)):

# check freq can be preserved on edge cases
if self.freq is not None:
Expand Down