@@ -1337,22 +1337,25 @@ def to_records(self, index=True, convert_datetime64=None):
1337
1337
"""
1338
1338
Convert DataFrame to a NumPy record array.
1339
1339
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
1341
1341
requested.
1342
1342
1343
1343
Parameters
1344
1344
----------
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
1348
1349
.. deprecated:: 0.23.0
1349
1350
1350
1351
Whether to convert the index to datetime.datetime if it is a
1351
1352
DatetimeIndex.
1352
1353
1353
1354
Returns
1354
1355
-------
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.
1356
1359
1357
1360
See Also
1358
1361
--------
@@ -1374,31 +1377,20 @@ def to_records(self, index=True, convert_datetime64=None):
1374
1377
rec.array([('a', 1, 0.5 ), ('b', 2, 0.75)],
1375
1378
dtype=[('index', 'O'), ('A', '<i8'), ('B', '<f8')])
1376
1379
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
+
1377
1389
The index can be excluded from the record array:
1378
1390
1379
1391
>>> df.to_records(index=False)
1380
1392
rec.array([(1, 0.5 ), (2, 0.75)],
1381
1393
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')])
1402
1394
"""
1403
1395
1404
1396
if convert_datetime64 is not None :
0 commit comments