-
-
Notifications
You must be signed in to change notification settings - Fork 18.2k
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
REF: IntervalIndex[IntervalArray] #20611
Changes from 19 commits
9b8564f
9e5fc50
abb8a45
4e48e88
11d97db
de96a61
24db222
d2bb35a
f22b453
934f238
097b9a4
c2bca65
39249a3
f82df72
e0fe0bc
7c0ffd3
e89d89a
a33940a
382737d
95f8f15
e82eeb6
bccb4f7
99ab41f
385ce59
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1924,11 +1924,23 @@ untouched. If the data is modified, it is because you did so explicitly. | |
dtypes | ||
------ | ||
|
||
The main types stored in pandas objects are ``float``, ``int``, ``bool``, | ||
``datetime64[ns]`` and ``datetime64[ns, tz]``, ``timedelta[ns]``, | ||
``category`` and ``object``. In addition these dtypes have item sizes, e.g. | ||
``int64`` and ``int32``. See :ref:`Series with TZ <timeseries.timezone_series>` | ||
for more detail on ``datetime64[ns, tz]`` dtypes. | ||
For the most part, pandas uses NumPy arrays and dtypes for Series or individual | ||
columns of a DataFrame. The main types allowed in pandas objects are ``float``, | ||
``int``, ``bool``, and ``datetime64[ns]`` (note that NumPy does not support | ||
timezone-aware datetimes). | ||
|
||
In addition to NumPy's types, pandas :ref:`extends <extending.extension-types>` | ||
NumPy's type-system for a few cases. | ||
|
||
* :ref:`Categorical <categorical>` | ||
* :ref:`Datetime with Timezone <timeseries.timezone_series>` | ||
* Interval | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. do we have a ref for this? (maybe should create one if not)? |
||
|
||
Pandas uses the ``object`` dtype for storing strings. | ||
|
||
Finally, arbitrary objects may be stored using the ``object`` dtype, but should | ||
be avoided to the extent possible (for performance and interoperability with | ||
other libraries and methods. See :ref:`basics.object_conversion`). | ||
|
||
A convenient :attr:`~DataFrame.dtypes` attribute for DataFrame returns a Series | ||
with the data type of each column. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -66,6 +66,43 @@ Current Behavior: | |
|
||
result | ||
|
||
|
||
.. _whatsnew_0240.enhancements.interval: | ||
|
||
Storing Interval Data in Series and DataFrame | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
Interval data may now be stored in a Series or DataFrame, in addition to an | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. double backticks on Series/DataFrame. |
||
:class:`IntervalIndex` like before (:issue:`19453`). | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. before -> previously. |
||
|
||
.. ipython:: python | ||
|
||
ser = pd.Series(pd.interval_range(0, 5)) | ||
ser | ||
ser.dtype | ||
|
||
Previously, these would be cast to a NumPy array of Interval objects. In general, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. double backticks around Interval, or use a class ref. & Series |
||
this should result in better performance when storing an array of intervals in | ||
a Series. | ||
|
||
Note that the ``.values`` of a Series containing intervals is no longer a NumPy | ||
array. Rather, it's an ``ExtensionArray``, composed of two arrays ``left`` and | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think you need to show the internal impl. (e.g. just the first sentence is ok) |
||
``right``. | ||
|
||
.. ipython:: python | ||
|
||
ser.values | ||
|
||
To recover the NumPy array of Interval objects, use :func:`numpy.asarray`: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if you are going to have the below section I would not use this here (showing asarray) as its duplicative, just point to the ref for the below section) |
||
|
||
.. ipython:: python | ||
|
||
np.asarray(ser.values) | ||
|
||
This is the same behavior as ``Series.values`` for categorical data. See | ||
:ref:`whatsnew_0240.api_breaking.interval_values` for more. | ||
|
||
|
||
.. _whatsnew_0240.enhancements.other: | ||
|
||
Other Enhancements | ||
|
@@ -90,6 +127,45 @@ Other Enhancements | |
Backwards incompatible API changes | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
|
||
|
||
.. _whatsnew_0240.api_breaking.interval_values: | ||
|
||
``IntervalIndex.values`` is now an ``IntervalArray`` | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
The ``.values`` attribute of an :class:`IntervalIndex` now returns an | ||
``IntervalArray``, rather than a NumPy array of :class:`Interval` objects | ||
(:issue:`19453`). | ||
|
||
Previous Behavior: | ||
|
||
.. code-block:: ipython | ||
|
||
In [1]: idx = pd.interval_range(0, 4) | ||
|
||
In [2]: idx.values | ||
Out[2]: | ||
array([Interval(0, 1, closed='right'), Interval(1, 2, closed='right'), | ||
Interval(2, 3, closed='right'), Interval(3, 4, closed='right')], | ||
dtype=object) | ||
|
||
New Behavior: | ||
|
||
.. ipython:: python | ||
|
||
idx = pd.interval_range(0, 4) | ||
idx.values | ||
|
||
This mirrors ``CateogricalIndex.values``, which returns a ``Categorical``. | ||
|
||
For situations where you need an ``ndarray`` of Interval objects, use | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. double backticks around Interval |
||
:meth:`numpy.asarray` or ``idx.astype(object)``. | ||
|
||
.. ipython:: python | ||
|
||
idx.values.astype(object) | ||
|
||
|
||
.. _whatsnew_0240.api.datetimelike.normalize: | ||
|
||
Tick DateOffset Normalize Restrictions | ||
|
@@ -345,6 +421,7 @@ Interval | |
^^^^^^^^ | ||
|
||
- Bug in the :class:`IntervalIndex` constructor where the ``closed`` parameter did not always override the inferred ``closed`` (:issue:`19370`) | ||
- Bug in the ``IntervalIndex`` repr missing a trailing comma at the end of the "data" section (:issue`20611`) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what does this mean? what is the 'data' section? that's an internal reference There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Cleaned this up. IIRC this was a side issue that @TomAugspurger found when he originally made the PR. A comma was missing after the list of intervals in the repr. |
||
- | ||
- | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,6 +20,7 @@ | |
_ensure_int64, | ||
_ensure_object, | ||
_ensure_platform_int, | ||
is_extension_array_dtype, | ||
is_dtype_equal, | ||
is_datetimelike, | ||
is_datetime64_dtype, | ||
|
@@ -1243,6 +1244,11 @@ def __array__(self, dtype=None): | |
ret = take_1d(self.categories.values, self._codes) | ||
if dtype and not is_dtype_equal(dtype, self.categories.dtype): | ||
return np.asarray(ret, dtype) | ||
if is_extension_array_dtype(ret): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. comment this. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have an update on this section already in intna |
||
# When we're a Categorical[ExtensionArray], like Interval, | ||
# we need to ensure __array__ get's all the way to an | ||
# ndarray. | ||
ret = np.asarray(ret) | ||
return ret | ||
|
||
def __setstate__(self, state): | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
also Periods ?