Skip to content

Commit 110c02f

Browse files
mroeschkejreback
authored andcommitted
DEPR: Series.put, Series.real, Series.imag, Index.dtype_str (#27106)
1 parent 497c4eb commit 110c02f

File tree

13 files changed

+64
-27
lines changed

13 files changed

+64
-27
lines changed

doc/source/whatsnew/v0.25.0.rst

+4-1
Original file line numberDiff line numberDiff line change
@@ -604,7 +604,10 @@ Other deprecations
604604
- The :meth:`Series.ftype`, :meth:`Series.ftypes` and :meth:`DataFrame.ftypes` methods are deprecated and will be removed in a future version.
605605
Instead, use :meth:`Series.dtype` and :meth:`DataFrame.dtypes` (:issue:`26705`).
606606
- :meth:`Timedelta.resolution` is deprecated and replaced with :meth:`Timedelta.resolution_string`. In a future version, :meth:`Timedelta.resolution` will be changed to behave like the standard library :attr:`timedelta.resolution` (:issue:`21344`)
607-
- func:`read_table` has been undeprecated. (:issue:`25220`)
607+
- :func:`read_table` has been undeprecated. (:issue:`25220`)
608+
- :attr:`Index.dtype_str` is deprecated. (:issue:`18262`)
609+
- :attr:`Series.imag` and :attr:`Series.real` are deprecated. (:issue:`18262`)
610+
- :meth:`Series.put` is deprecated. (:issue:`18262`)
608611

609612
.. _whatsnew_0250.prior_deprecations:
610613

pandas/core/arrays/categorical.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -1989,9 +1989,7 @@ def _repr_categories_info(self):
19891989
"""
19901990

19911991
category_strs = self._repr_categories()
1992-
dtype = getattr(self.categories, 'dtype_str',
1993-
str(self.categories.dtype))
1994-
1992+
dtype = str(self.categories.dtype)
19951993
levheader = "Categories ({length}, {dtype}): ".format(
19961994
length=len(self.categories), dtype=dtype)
19971995
width, height = get_terminal_size()

pandas/core/indexes/base.py

+5
Original file line numberDiff line numberDiff line change
@@ -690,7 +690,12 @@ def dtype(self):
690690
def dtype_str(self):
691691
"""
692692
Return the dtype str of the underlying data.
693+
694+
.. deprecated:: 0.25.0
693695
"""
696+
warnings.warn('`dtype_str` has been deprecated. Call `str` on the '
697+
'dtype attribute instead.', FutureWarning,
698+
stacklevel=2)
694699
return str(self.dtype)
695700

696701
def ravel(self, order='C'):

pandas/core/nanops.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1275,7 +1275,7 @@ def _ensure_numeric(x):
12751275
except (TypeError, ValueError):
12761276
x = x.astype(np.float64)
12771277
else:
1278-
if not np.any(x.imag):
1278+
if not np.any(np.imag(x)):
12791279
x = x.real
12801280
elif not (is_float(x) or is_integer(x) or is_complex(x)):
12811281
try:

pandas/core/series.py

+12
Original file line numberDiff line numberDiff line change
@@ -617,10 +617,14 @@ def put(self, *args, **kwargs):
617617
"""
618618
Apply the `put` method to its `values` attribute if it has one.
619619
620+
.. deprecated:: 0.25.0
621+
620622
See Also
621623
--------
622624
numpy.ndarray.put
623625
"""
626+
warnings.warn('`put` has been deprecated and will be removed in a'
627+
'future version.', FutureWarning, stacklevel=2)
624628
self._values.put(*args, **kwargs)
625629

626630
def __len__(self):
@@ -793,7 +797,11 @@ def __array_prepare__(self, result, context=None):
793797
def real(self):
794798
"""
795799
Return the real value of vector.
800+
801+
.. deprecated 0.25.0
796802
"""
803+
warnings.warn("`real` has be deprecated and will be removed in a "
804+
"future verison", FutureWarning, stacklevel=2)
797805
return self.values.real
798806

799807
@real.setter
@@ -804,7 +812,11 @@ def real(self, v):
804812
def imag(self):
805813
"""
806814
Return imag value of vector.
815+
816+
.. deprecated 0.25.0
807817
"""
818+
warnings.warn("`imag` has be deprecated and will be removed in a "
819+
"future verison", FutureWarning, stacklevel=2)
808820
return self.values.imag
809821

810822
@imag.setter

pandas/core/util/hashing.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ def hash_array(vals, encoding='utf8', hash_key=None, categorize=True):
269269
# we'll be working with everything as 64-bit values, so handle this
270270
# 128-bit value early
271271
if np.issubdtype(dtype, np.complex128):
272-
return hash_array(vals.real) + 23 * hash_array(vals.imag)
272+
return hash_array(np.real(vals)) + 23 * hash_array(np.imag(vals))
273273

274274
# First, turn whatever array this is into unsigned 64-bit ints, if we can
275275
# manage it.

pandas/io/packers.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -523,16 +523,16 @@ def encode(obj):
523523
return {'typ': 'np_scalar',
524524
'sub_typ': 'np_complex',
525525
'dtype': obj.dtype.name,
526-
'real': obj.real.__repr__(),
527-
'imag': obj.imag.__repr__()}
526+
'real': np.real(obj).__repr__(),
527+
'imag': np.imag(obj).__repr__()}
528528
else:
529529
return {'typ': 'np_scalar',
530530
'dtype': obj.dtype.name,
531531
'data': obj.__repr__()}
532532
elif isinstance(obj, complex):
533533
return {'typ': 'np_complex',
534-
'real': obj.real.__repr__(),
535-
'imag': obj.imag.__repr__()}
534+
'real': np.real(obj).__repr__(),
535+
'imag': np.imag(obj).__repr__()}
536536

537537
return obj
538538

pandas/tests/indexes/multi/test_format.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@
88

99

1010
def test_dtype_str(indices):
11-
dtype = indices.dtype_str
12-
assert isinstance(dtype, str)
13-
assert dtype == str(indices.dtype)
11+
with tm.assert_produces_warning(FutureWarning):
12+
dtype = indices.dtype_str
13+
assert isinstance(dtype, str)
14+
assert dtype == str(indices.dtype)
1415

1516

1617
def test_format(idx):

pandas/tests/indexes/period/test_period.py

+7-5
Original file line numberDiff line numberDiff line change
@@ -132,12 +132,14 @@ def test_shallow_copy_changing_freq_raises(self):
132132

133133
def test_dtype_str(self):
134134
pi = pd.PeriodIndex([], freq='M')
135-
assert pi.dtype_str == 'period[M]'
136-
assert pi.dtype_str == str(pi.dtype)
135+
with tm.assert_produces_warning(FutureWarning):
136+
assert pi.dtype_str == 'period[M]'
137+
assert pi.dtype_str == str(pi.dtype)
137138

138-
pi = pd.PeriodIndex([], freq='3M')
139-
assert pi.dtype_str == 'period[3M]'
140-
assert pi.dtype_str == str(pi.dtype)
139+
with tm.assert_produces_warning(FutureWarning):
140+
pi = pd.PeriodIndex([], freq='3M')
141+
assert pi.dtype_str == 'period[3M]'
142+
assert pi.dtype_str == str(pi.dtype)
141143

142144
def test_view_asi8(self):
143145
idx = pd.PeriodIndex([], freq='M')

pandas/tests/indexes/test_common.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -155,9 +155,10 @@ def test_set_name_methods(self, indices):
155155
assert indices.names == [name]
156156

157157
def test_dtype_str(self, indices):
158-
dtype = indices.dtype_str
159-
assert isinstance(dtype, str)
160-
assert dtype == str(indices.dtype)
158+
with tm.assert_produces_warning(FutureWarning):
159+
dtype = indices.dtype_str
160+
assert isinstance(dtype, str)
161+
assert dtype == str(indices.dtype)
161162

162163
def test_hash_error(self, indices):
163164
index = indices

pandas/tests/series/test_dtypes.py

+12-4
Original file line numberDiff line numberDiff line change
@@ -428,17 +428,25 @@ def test_astype_empty_constructor_equality(self, dtype):
428428
as_type_empty = Series([]).astype(dtype)
429429
tm.assert_series_equal(init_empty, as_type_empty)
430430

431+
@pytest.mark.filterwarnings('ignore::FutureWarning')
431432
def test_complex(self):
432433
# see gh-4819: complex access for ndarray compat
433434
a = np.arange(5, dtype=np.float64)
434435
b = Series(a + 4j * a)
435436

436-
tm.assert_numpy_array_equal(a, b.real)
437-
tm.assert_numpy_array_equal(4 * a, b.imag)
437+
tm.assert_numpy_array_equal(a, np.real(b))
438+
tm.assert_numpy_array_equal(4 * a, np.imag(b))
438439

439440
b.real = np.arange(5) + 5
440-
tm.assert_numpy_array_equal(a + 5, b.real)
441-
tm.assert_numpy_array_equal(4 * a, b.imag)
441+
tm.assert_numpy_array_equal(a + 5, np.real(b))
442+
tm.assert_numpy_array_equal(4 * a, np.imag(b))
443+
444+
def test_real_imag_deprecated(self):
445+
# GH 18262
446+
s = pd.Series([1])
447+
with tm.assert_produces_warning(FutureWarning):
448+
s.imag
449+
s.real
442450

443451
def test_arg_for_errors_in_astype(self):
444452
# see gh-14878

pandas/tests/series/test_internals.py

+7
Original file line numberDiff line numberDiff line change
@@ -221,3 +221,10 @@ def test_hasnans_unchached_for_series():
221221
ser.iloc[-1] = np.nan
222222
assert ser.hasnans is True
223223
assert Series.hasnans.__doc__ == pd.Index.hasnans.__doc__
224+
225+
226+
def test_put_deprecated():
227+
# GH 18262
228+
s = pd.Series([1])
229+
with tm.assert_produces_warning(FutureWarning):
230+
s.put(0, 0)

pandas/tests/test_nanops.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -144,9 +144,9 @@ def _coerce_tds(targ, res):
144144
# but nanops doesn't, so make that an exception
145145
elif targ.dtype.kind == 'O':
146146
raise
147-
tm.assert_almost_equal(targ.real, res.real,
147+
tm.assert_almost_equal(np.real(targ), np.real(res),
148148
check_dtype=check_dtype)
149-
tm.assert_almost_equal(targ.imag, res.imag,
149+
tm.assert_almost_equal(np.imag(targ), np.imag(res),
150150
check_dtype=check_dtype)
151151

152152
def check_fun_data(self, testfunc, targfunc, testarval, targarval,

0 commit comments

Comments
 (0)