Skip to content

Commit bcf416c

Browse files
Update to_records docstring.
- Minor changes (missing dots, newlines) to make tests pass. - More examples.
1 parent d7bcb22 commit bcf416c

File tree

1 file changed

+42
-4
lines changed

1 file changed

+42
-4
lines changed

pandas/core/frame.py

+42-4
Original file line numberDiff line numberDiff line change
@@ -1209,20 +1209,58 @@ def from_records(cls, data, index=None, exclude=None, columns=None,
12091209

12101210
def to_records(self, index=True, convert_datetime64=True):
12111211
"""
1212-
Convert DataFrame to record array. Index will be put in the
1213-
'index' field of the record array if requested
1212+
Convert DataFrame to record array.
1213+
1214+
Index will be put in the 'index' field of the record array if requested.
12141215
12151216
Parameters
12161217
----------
12171218
index : boolean, default True
1218-
Include index in resulting record array, stored in 'index' field
1219+
Include index in resulting record array, stored in 'index' field.
12191220
convert_datetime64 : boolean, default True
12201221
Whether to convert the index to datetime.datetime if it is a
1221-
DatetimeIndex
1222+
DatetimeIndex.
12221223
12231224
Returns
12241225
-------
12251226
y : recarray
1227+
1228+
See Also
1229+
--------
1230+
DataFrame.from_records: convert structured or record ndarray to DataFrame.
1231+
1232+
Examples
1233+
--------
1234+
>>> df = pd.DataFrame({'col1': [1, 2], 'col2': [0.5, 0.75]}, index=['a', 'b'])
1235+
>>> df
1236+
col1 col2
1237+
a 1 0.50
1238+
b 2 0.75
1239+
>>> df.to_records()
1240+
rec.array([('a', 1, 0.5 ), ('b', 2, 0.75)],
1241+
dtype=[('index', 'O'), ('col1', '<i8'), ('col2', '<f8')])
1242+
1243+
The index can be excluded from the record array:
1244+
>>> df.to_records(index=False)
1245+
rec.array([(1, 0.5 ), (2, 0.75)],
1246+
dtype=[('col1', '<i8'), ('col2', '<f8')])
1247+
1248+
By default, timestamps are converted to `datetime.datetime` objects
1249+
>>> df.index = pd.date_range('2018-01-01 09:00', periods=2, freq='min')
1250+
>>> df
1251+
col1 col2
1252+
2018-01-01 09:00:00 1 0.50
1253+
2018-01-01 09:01:00 2 0.75
1254+
>>> df.to_records()
1255+
rec.array([(datetime.datetime(2018, 1, 1, 9, 0), 1, 0.5 ),
1256+
(datetime.datetime(2018, 1, 1, 9, 1), 2, 0.75)],
1257+
dtype=[('index', 'O'), ('col1', '<i8'), ('col2', '<f8')])
1258+
1259+
The timestamp conversion can be disabled to use NumPy datetime64 objects instead:
1260+
>>> df.to_records(convert_datetime64=False)
1261+
rec.array([('2018-01-01T09:00:00.000000000', 1, 0.5 ),
1262+
('2018-01-01T09:01:00.000000000', 2, 0.75)],
1263+
dtype=[('index', '<M8[ns]'), ('col1', '<i8'), ('col2', '<f8')])
12261264
"""
12271265
if index:
12281266
if is_datetime64_any_dtype(self.index) and convert_datetime64:

0 commit comments

Comments
 (0)