Skip to content

Commit 5abb8c3

Browse files
topper-123vaibhavhrt
authored andcommitted
use range in RangeIndex instead of _start etc. (pandas-dev#26581)
1 parent b642726 commit 5abb8c3

File tree

8 files changed

+202
-200
lines changed

8 files changed

+202
-200
lines changed

doc/source/whatsnew/v0.25.0.rst

+3
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,9 @@ Other Deprecations
476476
the :meth:`SparseArray.to_dense` method instead (:issue:`26421`).
477477
- The functions :func:`pandas.to_datetime` and :func:`pandas.to_timedelta` have deprecated the ``box`` keyword. Instead, use :meth:`to_numpy` or :meth:`Timestamp.to_datetime64` or :meth:`Timedelta.to_timedelta64`. (:issue:`24416`)
478478
- The :meth:`DataFrame.compound` and :meth:`Series.compound` methods are deprecated and will be removed in a future version (:issue:`26405`).
479+
- The internal attributes ``_start``, ``_stop`` and ``_step`` attributes of :class:`RangeIndex` have been deprecated.
480+
Use the public attributes :attr:`~RangeIndex.start`, :attr:`~RangeIndex.stop` and :attr:`~RangeIndex.step` instead (:issue:`26581`).
481+
479482

480483
.. _whatsnew_0250.prior_deprecations:
481484

pandas/core/dtypes/common.py

+29
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
""" common type operations """
2+
from typing import Union
23
import warnings
34

45
import numpy as np
@@ -125,6 +126,34 @@ def ensure_int_or_float(arr: ArrayLike, copy=False) -> np.array:
125126
return arr.astype('float64', copy=copy)
126127

127128

129+
def ensure_python_int(value: Union[int, np.integer]) -> int:
130+
"""
131+
Ensure that a value is a python int.
132+
133+
Parameters
134+
----------
135+
value: int or numpy.integer
136+
137+
Returns
138+
-------
139+
int
140+
141+
Raises
142+
------
143+
TypeError: if the value isn't an int or can't be converted to one.
144+
"""
145+
if not is_scalar(value):
146+
raise TypeError("Value needs to be a scalar value, was type {}"
147+
.format(type(value)))
148+
msg = "Wrong type {} for value {}"
149+
try:
150+
new_value = int(value)
151+
assert (new_value == value)
152+
except (TypeError, ValueError, AssertionError):
153+
raise TypeError(msg.format(type(value), value))
154+
return new_value
155+
156+
128157
def classes(*klasses):
129158
""" evaluate if the tipo is a subclass of the klasses """
130159
return lambda tipo: issubclass(tipo, klasses)

pandas/core/dtypes/concat.py

+11-10
Original file line numberDiff line numberDiff line change
@@ -541,36 +541,37 @@ def _concat_rangeindex_same_dtype(indexes):
541541
"""
542542
from pandas import Int64Index, RangeIndex
543543

544-
start = step = next = None
544+
start = step = next_ = None
545545

546546
# Filter the empty indexes
547547
non_empty_indexes = [obj for obj in indexes if len(obj)]
548548

549549
for obj in non_empty_indexes:
550+
rng = obj._range # type: range
550551

551552
if start is None:
552553
# This is set by the first non-empty index
553-
start = obj._start
554-
if step is None and len(obj) > 1:
555-
step = obj._step
554+
start = rng.start
555+
if step is None and len(rng) > 1:
556+
step = rng.step
556557
elif step is None:
557558
# First non-empty index had only one element
558-
if obj._start == start:
559+
if rng.start == start:
559560
return _concat_index_same_dtype(indexes, klass=Int64Index)
560-
step = obj._start - start
561+
step = rng.start - start
561562

562-
non_consecutive = ((step != obj._step and len(obj) > 1) or
563-
(next is not None and obj._start != next))
563+
non_consecutive = ((step != rng.step and len(rng) > 1) or
564+
(next_ is not None and rng.start != next_))
564565
if non_consecutive:
565566
return _concat_index_same_dtype(indexes, klass=Int64Index)
566567

567568
if step is not None:
568-
next = obj[-1] + step
569+
next_ = rng[-1] + step
569570

570571
if non_empty_indexes:
571572
# Get the stop value from "next" or alternatively
572573
# from the last non-empty index
573-
stop = non_empty_indexes[-1]._stop if next is None else next
574+
stop = non_empty_indexes[-1].stop if next_ is None else next_
574575
return RangeIndex(start, stop, step)
575576

576577
# Here all "indexes" had 0 length, i.e. were empty.

pandas/core/frame.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -2282,7 +2282,7 @@ def info(self, verbose=None, buf=None, max_cols=None, memory_usage=None,
22822282
text_col 5 non-null object
22832283
float_col 5 non-null float64
22842284
dtypes: float64(1), int64(1), object(1)
2285-
memory usage: 200.0+ bytes
2285+
memory usage: 248.0+ bytes
22862286
22872287
Prints a summary of columns count and its dtypes but not per column
22882288
information:
@@ -2292,7 +2292,7 @@ def info(self, verbose=None, buf=None, max_cols=None, memory_usage=None,
22922292
RangeIndex: 5 entries, 0 to 4
22932293
Columns: 3 entries, int_col to float_col
22942294
dtypes: float64(1), int64(1), object(1)
2295-
memory usage: 200.0+ bytes
2295+
memory usage: 248.0+ bytes
22962296
22972297
Pipe output of DataFrame.info to buffer instead of sys.stdout, get
22982298
buffer content and writes to a text file:
@@ -2494,7 +2494,7 @@ def memory_usage(self, index=True, deep=False):
24942494
4 1 1.0 1.0+0.0j 1 True
24952495
24962496
>>> df.memory_usage()
2497-
Index 80
2497+
Index 128
24982498
int64 40000
24992499
float64 40000
25002500
complex128 80000
@@ -2513,7 +2513,7 @@ def memory_usage(self, index=True, deep=False):
25132513
The memory footprint of `object` dtype columns is ignored by default:
25142514
25152515
>>> df.memory_usage(deep=True)
2516-
Index 80
2516+
Index 128
25172517
int64 40000
25182518
float64 40000
25192519
complex128 80000
@@ -2525,7 +2525,7 @@ def memory_usage(self, index=True, deep=False):
25252525
many repeated values.
25262526
25272527
>>> df['object'].astype('category').memory_usage(deep=True)
2528-
5168
2528+
5216
25292529
"""
25302530
result = Series([c.memory_usage(index=False, deep=deep)
25312531
for col, c in self.iteritems()], index=self.columns)

0 commit comments

Comments
 (0)