Skip to content

Commit 6d7272a

Browse files
samuelsinayokoTomAugspurger
authored andcommitted
DOC: update DataFrame.to_records (#20191)
* Update to_records docstring. - Minor changes (missing dots, newlines) to make tests pass. - More examples. * Fix html docs. Missing newlines. * Reword datetime type information. * flake8 errors * Fix typo (duplicated type) * Remove unwanted blank line after Examples. * Fix doctests. ``` (pandas_dev) sinayoks@landade:~/dev/pandas/ $ pytest --doctest-modules pandas/core/frame.py -k to_record ========================================================================================== test session starts ========================================================================================== platform darwin -- Python 3.6.4, pytest-3.4.2, py-1.5.2, pluggy-0.6.0 rootdir: /Users/sinayoks/dev/pandas, inifile: setup.cfg plugins: xdist-1.22.1, forked-0.2, cov-2.5.1 collected 43 items pandas/core/frame.py . [100%] ========================================================================================== 42 tests deselected ========================================================================================== ``` * Few more changes
1 parent 636335a commit 6d7272a

File tree

1 file changed

+53
-5
lines changed

1 file changed

+53
-5
lines changed

pandas/core/frame.py

+53-5
Original file line numberDiff line numberDiff line change
@@ -1209,20 +1209,68 @@ 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 a NumPy record array.
1213+
1214+
Index will be put in the 'index' field of the record array if
1215+
requested.
12141216
12151217
Parameters
12161218
----------
12171219
index : boolean, default True
1218-
Include index in resulting record array, stored in 'index' field
1220+
Include index in resulting record array, stored in 'index' field.
12191221
convert_datetime64 : boolean, default True
12201222
Whether to convert the index to datetime.datetime if it is a
1221-
DatetimeIndex
1223+
DatetimeIndex.
12221224
12231225
Returns
12241226
-------
1225-
y : recarray
1227+
y : numpy.recarray
1228+
1229+
See Also
1230+
--------
1231+
DataFrame.from_records: convert structured or record ndarray
1232+
to DataFrame.
1233+
numpy.recarray: ndarray that allows field access using
1234+
attributes, analogous to typed columns in a
1235+
spreadsheet.
1236+
1237+
Examples
1238+
--------
1239+
>>> df = pd.DataFrame({'A': [1, 2], 'B': [0.5, 0.75]},
1240+
... index=['a', 'b'])
1241+
>>> df
1242+
A B
1243+
a 1 0.50
1244+
b 2 0.75
1245+
>>> df.to_records()
1246+
rec.array([('a', 1, 0.5 ), ('b', 2, 0.75)],
1247+
dtype=[('index', 'O'), ('A', '<i8'), ('B', '<f8')])
1248+
1249+
The index can be excluded from the record array:
1250+
1251+
>>> df.to_records(index=False)
1252+
rec.array([(1, 0.5 ), (2, 0.75)],
1253+
dtype=[('A', '<i8'), ('B', '<f8')])
1254+
1255+
By default, timestamps are converted to `datetime.datetime`:
1256+
1257+
>>> df.index = pd.date_range('2018-01-01 09:00', periods=2, freq='min')
1258+
>>> df
1259+
A B
1260+
2018-01-01 09:00:00 1 0.50
1261+
2018-01-01 09:01:00 2 0.75
1262+
>>> df.to_records()
1263+
rec.array([(datetime.datetime(2018, 1, 1, 9, 0), 1, 0.5 ),
1264+
(datetime.datetime(2018, 1, 1, 9, 1), 2, 0.75)],
1265+
dtype=[('index', 'O'), ('A', '<i8'), ('B', '<f8')])
1266+
1267+
The timestamp conversion can be disabled so NumPy's datetime64
1268+
data type is used instead:
1269+
1270+
>>> df.to_records(convert_datetime64=False)
1271+
rec.array([('2018-01-01T09:00:00.000000000', 1, 0.5 ),
1272+
('2018-01-01T09:01:00.000000000', 2, 0.75)],
1273+
dtype=[('index', '<M8[ns]'), ('A', '<i8'), ('B', '<f8')])
12261274
"""
12271275
if index:
12281276
if is_datetime64_any_dtype(self.index) and convert_datetime64:

0 commit comments

Comments
 (0)