Skip to content

Commit 370054e

Browse files
DOC: fix old whatsnew examples + few sphinx warnings (#26780)
1 parent ea06f8d commit 370054e

File tree

5 files changed

+189
-59
lines changed

5 files changed

+189
-59
lines changed

doc/source/whatsnew/v0.11.0.rst

+143-38
Original file line numberDiff line numberDiff line change
@@ -105,27 +105,54 @@ Conversion
105105
106106
Mixed Conversion
107107

108-
.. ipython:: python
109-
:okwarning:
108+
.. code-block:: ipython
110109
111-
df3['D'] = '1.'
112-
df3['E'] = '1'
113-
df3.convert_objects(convert_numeric=True).dtypes
110+
In [12]: df3['D'] = '1.'
114111
115-
# same, but specific dtype conversion
116-
df3['D'] = df3['D'].astype('float16')
117-
df3['E'] = df3['E'].astype('int32')
118-
df3.dtypes
112+
In [13]: df3['E'] = '1'
113+
114+
In [14]: df3.convert_objects(convert_numeric=True).dtypes
115+
Out[14]:
116+
A float32
117+
B float64
118+
C float64
119+
D float64
120+
E int64
121+
dtype: object
122+
123+
# same, but specific dtype conversion
124+
In [15]: df3['D'] = df3['D'].astype('float16')
125+
126+
In [16]: df3['E'] = df3['E'].astype('int32')
127+
128+
In [17]: df3.dtypes
129+
Out[17]:
130+
A float32
131+
B float64
132+
C float64
133+
D float16
134+
E int32
135+
dtype: object
119136
120137
Forcing Date coercion (and setting ``NaT`` when not datelike)
121138

122-
.. ipython:: python
123-
:okwarning:
139+
.. code-block:: ipython
140+
141+
In [18]: import datetime
142+
143+
In [19]: s = pd.Series([datetime.datetime(2001, 1, 1, 0, 0), 'foo', 1.0, 1,
144+
....: pd.Timestamp('20010104'), '20010105'], dtype='O')
145+
....:
124146
125-
import datetime
126-
s = pd.Series([datetime.datetime(2001, 1, 1, 0, 0), 'foo', 1.0, 1,
127-
pd.Timestamp('20010104'), '20010105'], dtype='O')
128-
s.convert_objects(convert_dates='coerce')
147+
In [20]: s.convert_objects(convert_dates='coerce')
148+
Out[20]:
149+
0 2001-01-01
150+
1 NaT
151+
2 NaT
152+
3 NaT
153+
4 2001-01-04
154+
5 2001-01-05
155+
dtype: datetime64[ns]
129156
130157
Dtype Gotchas
131158
~~~~~~~~~~~~~
@@ -138,11 +165,22 @@ dtypes, they *WILL* be respected, however (:issue:`2837`)
138165

139166
The following will all result in ``int64`` dtypes
140167

141-
.. ipython:: python
168+
.. code-block:: ipython
169+
170+
In [21]: pd.DataFrame([1, 2], columns=['a']).dtypes
171+
Out[21]:
172+
a int64
173+
dtype: object
174+
175+
In [22]: pd.DataFrame({'a': [1, 2]}).dtypes
176+
Out[22]:
177+
a int64
178+
dtype: object
142179
143-
pd.DataFrame([1, 2], columns=['a']).dtypes
144-
pd.DataFrame({'a': [1, 2]}).dtypes
145-
pd.DataFrame({'a': 1}, index=range(2)).dtypes
180+
In [23]: pd.DataFrame({'a': 1}, index=range(2)).dtypes
181+
Out[23]:
182+
a int64
183+
dtype: object
146184
147185
Keep in mind that ``DataFrame(np.array([1,2]))`` **WILL** result in ``int32`` on 32-bit platforms!
148186

@@ -152,28 +190,95 @@ Keep in mind that ``DataFrame(np.array([1,2]))`` **WILL** result in ``int32`` on
152190
Performing indexing operations on integer type data can easily upcast the data.
153191
The dtype of the input data will be preserved in cases where ``nans`` are not introduced.
154192

155-
.. ipython:: python
156-
157-
dfi = df3.astype('int32')
158-
dfi['D'] = dfi['D'].astype('int64')
159-
dfi
160-
dfi.dtypes
161-
162-
casted = dfi[dfi > 0]
163-
casted
164-
casted.dtypes
193+
.. code-block:: ipython
194+
195+
In [24]: dfi = df3.astype('int32')
196+
197+
In [25]: dfi['D'] = dfi['D'].astype('int64')
198+
199+
In [26]: dfi
200+
Out[26]:
201+
A B C D E
202+
0 0 0 0 1 1
203+
1 -2 0 1 1 1
204+
2 -2 0 2 1 1
205+
3 0 -1 3 1 1
206+
4 1 0 4 1 1
207+
5 0 0 5 1 1
208+
6 0 -1 6 1 1
209+
7 0 0 7 1 1
210+
211+
In [27]: dfi.dtypes
212+
Out[27]:
213+
A int32
214+
B int32
215+
C int32
216+
D int64
217+
E int32
218+
dtype: object
219+
220+
In [28]: casted = dfi[dfi > 0]
221+
222+
In [29]: casted
223+
Out[29]:
224+
A B C D E
225+
0 NaN NaN NaN 1 1
226+
1 NaN NaN 1.0 1 1
227+
2 NaN NaN 2.0 1 1
228+
3 NaN NaN 3.0 1 1
229+
4 1.0 NaN 4.0 1 1
230+
5 NaN NaN 5.0 1 1
231+
6 NaN NaN 6.0 1 1
232+
7 NaN NaN 7.0 1 1
233+
234+
In [30]: casted.dtypes
235+
Out[30]:
236+
A float64
237+
B float64
238+
C float64
239+
D int64
240+
E int32
241+
dtype: object
165242
166243
While float dtypes are unchanged.
167244

168-
.. ipython:: python
169-
170-
df4 = df3.copy()
171-
df4['A'] = df4['A'].astype('float32')
172-
df4.dtypes
173-
174-
casted = df4[df4 > 0]
175-
casted
176-
casted.dtypes
245+
.. code-block:: ipython
246+
247+
In [31]: df4 = df3.copy()
248+
249+
In [32]: df4['A'] = df4['A'].astype('float32')
250+
251+
In [33]: df4.dtypes
252+
Out[33]:
253+
A float32
254+
B float64
255+
C float64
256+
D float16
257+
E int32
258+
dtype: object
259+
260+
In [34]: casted = df4[df4 > 0]
261+
262+
In [35]: casted
263+
Out[35]:
264+
A B C D E
265+
0 NaN NaN NaN 1.0 1
266+
1 NaN 0.567020 1.0 1.0 1
267+
2 NaN 0.276232 2.0 1.0 1
268+
3 NaN NaN 3.0 1.0 1
269+
4 1.933792 NaN 4.0 1.0 1
270+
5 NaN 0.113648 5.0 1.0 1
271+
6 NaN NaN 6.0 1.0 1
272+
7 NaN 0.524988 7.0 1.0 1
273+
274+
In [36]: casted.dtypes
275+
Out[36]:
276+
A float32
277+
B float64
278+
C float64
279+
D float16
280+
E int32
281+
dtype: object
177282
178283
Datetimes Conversion
179284
~~~~~~~~~~~~~~~~~~~~

doc/source/whatsnew/v0.13.0.rst

+33-11
Original file line numberDiff line numberDiff line change
@@ -271,17 +271,39 @@ This is like an ``append`` operation.
271271
272272
A Panel setting operation on an arbitrary axis aligns the input to the Panel
273273

274-
.. ipython:: python
275-
:okwarning:
276-
277-
p = pd.Panel(np.arange(16).reshape(2, 4, 2),
278-
items=['Item1', 'Item2'],
279-
major_axis=pd.date_range('2001/1/12', periods=4),
280-
minor_axis=['A', 'B'], dtype='float64')
281-
p
282-
p.loc[:, :, 'C'] = pd.Series([30, 32], index=p.items)
283-
p
284-
p.loc[:, :, 'C']
274+
.. code-block:: ipython
275+
276+
In [20]: p = pd.Panel(np.arange(16).reshape(2, 4, 2),
277+
....: items=['Item1', 'Item2'],
278+
....: major_axis=pd.date_range('2001/1/12', periods=4),
279+
....: minor_axis=['A', 'B'], dtype='float64')
280+
....:
281+
282+
In [21]: p
283+
Out[21]:
284+
<class 'pandas.core.panel.Panel'>
285+
Dimensions: 2 (items) x 4 (major_axis) x 2 (minor_axis)
286+
Items axis: Item1 to Item2
287+
Major_axis axis: 2001-01-12 00:00:00 to 2001-01-15 00:00:00
288+
Minor_axis axis: A to B
289+
290+
In [22]: p.loc[:, :, 'C'] = pd.Series([30, 32], index=p.items)
291+
292+
In [23]: p
293+
Out[23]:
294+
<class 'pandas.core.panel.Panel'>
295+
Dimensions: 2 (items) x 4 (major_axis) x 3 (minor_axis)
296+
Items axis: Item1 to Item2
297+
Major_axis axis: 2001-01-12 00:00:00 to 2001-01-15 00:00:00
298+
Minor_axis axis: A to C
299+
300+
In [24]: p.loc[:, :, 'C']
301+
Out[24]:
302+
Item1 Item2
303+
2001-01-12 30.0 32.0
304+
2001-01-13 30.0 32.0
305+
2001-01-14 30.0 32.0
306+
2001-01-15 30.0 32.0
285307
286308
Float64Index API Change
287309
~~~~~~~~~~~~~~~~~~~~~~~

doc/source/whatsnew/v0.25.0.rst

+9-10
Original file line numberDiff line numberDiff line change
@@ -146,29 +146,28 @@ Constructing a :class:`MultiIndex` with ``NaN`` levels or codes value < -1 was a
146146
Now, construction with codes value < -1 is not allowed and ``NaN`` levels' corresponding codes
147147
would be reassigned as -1. (:issue:`19387`)
148148

149-
.. ipython:: python
150-
151-
mi1 = pd.MultiIndex(levels=[[np.nan, None, pd.NaT, 128, 2]],
152-
codes=[[0, -1, 1, 2, 3, 4]])
153-
mi2 = pd.MultiIndex(levels=[[1, 2]], codes=[[0, -2]])
154-
155149
*Previous Behavior*:
156150

157151
.. code-block:: ipython
158152
159-
In [1]: mi1
153+
In [1]: pd.MultiIndex(levels=[[np.nan, None, pd.NaT, 128, 2]],
154+
...: codes=[[0, -1, 1, 2, 3, 4]])
155+
...:
160156
Out[1]: MultiIndex(levels=[[nan, None, NaT, 128, 2]],
161157
codes=[[0, -1, 1, 2, 3, 4]])
162-
In [2]: mi2
158+
159+
In [2]: pd.MultiIndex(levels=[[1, 2]], codes=[[0, -2]])
163160
Out[2]: MultiIndex(levels=[[1, 2]],
164161
codes=[[0, -2]])
165162
166163
*New Behavior*:
167164

168165
.. ipython:: python
166+
:okexcept:
169167
170-
mi1
171-
mi2
168+
pd.MultiIndex(levels=[[np.nan, None, pd.NaT, 128, 2]],
169+
codes=[[0, -1, 1, 2, 3, 4]])
170+
pd.MultiIndex(levels=[[1, 2]], codes=[[0, -2]])
172171
173172
174173
.. _whatsnew_0250.api_breaking.groupby_apply_first_group_once:

pandas/core/frame.py

+3
Original file line numberDiff line numberDiff line change
@@ -4000,6 +4000,7 @@ def rename(self, *args, **kwargs):
40004000
intent.
40014001
40024002
Rename columns using a mapping:
4003+
40034004
>>> df = pd.DataFrame({"A": [1, 2, 3], "B": [4, 5, 6]})
40044005
>>> df.rename(columns={"A": "a", "B": "c"})
40054006
a c
@@ -4008,13 +4009,15 @@ def rename(self, *args, **kwargs):
40084009
2 3 6
40094010
40104011
Rename index using a mapping:
4012+
40114013
>>> df.rename(index={0: "x", 1: "y", 2: "z"})
40124014
A B
40134015
x 1 4
40144016
y 2 5
40154017
z 3 6
40164018
40174019
Cast index labels to a different type:
4020+
40184021
>>> df.index
40194022
RangeIndex(start=0, stop=3, step=1)
40204023
>>> df.rename(index=str).index

pandas/core/generic.py

+1
Original file line numberDiff line numberDiff line change
@@ -10867,6 +10867,7 @@ def _doc_parms(cls):
1086710867
level_output_1=0)
1086810868

1086910869
_stat_func_see_also = """
10870+
1087010871
See Also
1087110872
--------
1087210873
Series.sum : Return the sum.

0 commit comments

Comments
 (0)