Skip to content

Commit 68273a7

Browse files
H0R5Edatapythonista
authored andcommitted
DOC: Fix Dataframe.to_records examples (#22419)
1 parent 9c35865 commit 68273a7

File tree

2 files changed

+18
-26
lines changed

2 files changed

+18
-26
lines changed

ci/doctests.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ if [ "$DOCTEST" ]; then
2121

2222
# DataFrame / Series docstrings
2323
pytest --doctest-modules -v pandas/core/frame.py \
24-
-k"-assign -axes -combine -isin -itertuples -join -nlargest -nsmallest -nunique -pivot_table -quantile -query -reindex -reindex_axis -replace -round -set_index -stack -to_dict -to_records -to_stata -transform"
24+
-k"-assign -axes -combine -isin -itertuples -join -nlargest -nsmallest -nunique -pivot_table -quantile -query -reindex -reindex_axis -replace -round -set_index -stack -to_dict -to_stata -transform"
2525

2626
if [ $? -ne "0" ]; then
2727
RET=1

pandas/core/frame.py

+17-25
Original file line numberDiff line numberDiff line change
@@ -1337,22 +1337,25 @@ def to_records(self, index=True, convert_datetime64=None):
13371337
"""
13381338
Convert DataFrame to a NumPy record array.
13391339
1340-
Index will be put in the 'index' field of the record array if
1340+
Index will be included as the first field of the record array if
13411341
requested.
13421342
13431343
Parameters
13441344
----------
1345-
index : boolean, default True
1346-
Include index in resulting record array, stored in 'index' field.
1347-
convert_datetime64 : boolean, default None
1345+
index : bool, default True
1346+
Include index in resulting record array, stored in 'index'
1347+
field or using the index label, if set.
1348+
convert_datetime64 : bool, default None
13481349
.. deprecated:: 0.23.0
13491350
13501351
Whether to convert the index to datetime.datetime if it is a
13511352
DatetimeIndex.
13521353
13531354
Returns
13541355
-------
1355-
y : numpy.recarray
1356+
numpy.recarray
1357+
NumPy ndarray with the DataFrame labels as fields and each row
1358+
of the DataFrame as entries.
13561359
13571360
See Also
13581361
--------
@@ -1374,31 +1377,20 @@ def to_records(self, index=True, convert_datetime64=None):
13741377
rec.array([('a', 1, 0.5 ), ('b', 2, 0.75)],
13751378
dtype=[('index', 'O'), ('A', '<i8'), ('B', '<f8')])
13761379
1380+
If the DataFrame index has no label then the recarray field name
1381+
is set to 'index'. If the index has a label then this is used as the
1382+
field name:
1383+
1384+
>>> df.index = df.index.rename("I")
1385+
>>> df.to_records()
1386+
rec.array([('a', 1, 0.5 ), ('b', 2, 0.75)],
1387+
dtype=[('I', 'O'), ('A', '<i8'), ('B', '<f8')])
1388+
13771389
The index can be excluded from the record array:
13781390
13791391
>>> df.to_records(index=False)
13801392
rec.array([(1, 0.5 ), (2, 0.75)],
13811393
dtype=[('A', '<i8'), ('B', '<f8')])
1382-
1383-
By default, timestamps are converted to `datetime.datetime`:
1384-
1385-
>>> df.index = pd.date_range('2018-01-01 09:00', periods=2, freq='min')
1386-
>>> df
1387-
A B
1388-
2018-01-01 09:00:00 1 0.50
1389-
2018-01-01 09:01:00 2 0.75
1390-
>>> df.to_records()
1391-
rec.array([(datetime.datetime(2018, 1, 1, 9, 0), 1, 0.5 ),
1392-
(datetime.datetime(2018, 1, 1, 9, 1), 2, 0.75)],
1393-
dtype=[('index', 'O'), ('A', '<i8'), ('B', '<f8')])
1394-
1395-
The timestamp conversion can be disabled so NumPy's datetime64
1396-
data type is used instead:
1397-
1398-
>>> df.to_records(convert_datetime64=False)
1399-
rec.array([('2018-01-01T09:00:00.000000000', 1, 0.5 ),
1400-
('2018-01-01T09:01:00.000000000', 2, 0.75)],
1401-
dtype=[('index', '<M8[ns]'), ('A', '<i8'), ('B', '<f8')])
14021394
"""
14031395

14041396
if convert_datetime64 is not None:

0 commit comments

Comments
 (0)