@@ -1211,7 +1211,8 @@ def to_records(self, index=True, convert_datetime64=True):
1211
1211
"""
1212
1212
Convert DataFrame to record array.
1213
1213
1214
- Index will be put in the 'index' field of the record array if requested.
1214
+ Index will be put in the 'index' field of the record array if
1215
+ requested.
1215
1216
1216
1217
Parameters
1217
1218
----------
@@ -1227,44 +1228,50 @@ def to_records(self, index=True, convert_datetime64=True):
1227
1228
1228
1229
See Also
1229
1230
--------
1230
- DataFrame.from_records: convert structured or record ndarray to DataFrame.
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 (typed) columns in a
1235
+ spreadsheet.
1231
1236
1232
1237
Examples
1233
1238
--------
1234
1239
1235
- >>> df = pd.DataFrame({'col1': [1, 2], 'col2': [0.5, 0.75]}, index=['a', 'b'])
1240
+ >>> df = pd.DataFrame({'A': [1, 2], 'B': [0.5, 0.75]},
1241
+ ... index=['a', 'b'])
1236
1242
>>> df
1237
- col1 col2
1243
+ A B
1238
1244
a 1 0.50
1239
1245
b 2 0.75
1240
1246
>>> df.to_records()
1241
1247
rec.array([('a', 1, 0.5 ), ('b', 2, 0.75)],
1242
- dtype=[('index', 'O'), ('col1 ', '<i8'), ('col2 ', '<f8')])
1248
+ dtype=[('index', 'O'), ('A ', '<i8'), ('B ', '<f8')])
1243
1249
1244
1250
The index can be excluded from the record array:
1245
1251
1246
1252
>>> df.to_records(index=False)
1247
1253
rec.array([(1, 0.5 ), (2, 0.75)],
1248
- dtype=[('col1 ', '<i8'), ('col2 ', '<f8')])
1254
+ dtype=[('A ', '<i8'), ('B ', '<f8')])
1249
1255
1250
1256
By default, timestamps are converted to `datetime.datetime`:
1251
1257
1252
1258
>>> df.index = pd.date_range('2018-01-01 09:00', periods=2, freq='min')
1253
1259
>>> df
1254
- col1 col2
1260
+ A B
1255
1261
2018-01-01 09:00:00 1 0.50
1256
1262
2018-01-01 09:01:00 2 0.75
1257
1263
>>> df.to_records()
1258
1264
rec.array([(datetime.datetime(2018, 1, 1, 9, 0), 1, 0.5 ),
1259
1265
(datetime.datetime(2018, 1, 1, 9, 1), 2, 0.75)],
1260
- dtype=[('index', 'O'), ('col1 ', '<i8'), ('col2 ', '<f8')])
1266
+ dtype=[('index', 'O'), ('A ', '<i8'), ('B ', '<f8')])
1261
1267
1262
- The timestamp conversion can be disabled so NumPy's datetime64 data type is used instead:
1268
+ The timestamp conversion can be disabled so NumPy's datetime64
1269
+ data type is used instead:
1263
1270
1264
1271
>>> df.to_records(convert_datetime64=False)
1265
1272
rec.array([('2018-01-01T09:00:00.000000000', 1, 0.5 ),
1266
1273
('2018-01-01T09:01:00.000000000', 2, 0.75)],
1267
- dtype=[('index', '<M8[ns]'), ('col1 ', '<i8'), ('col2 ', '<f8')])
1274
+ dtype=[('index', '<M8[ns]'), ('A ', '<i8'), ('B ', '<f8')])
1268
1275
"""
1269
1276
if index :
1270
1277
if is_datetime64_any_dtype (self .index ) and convert_datetime64 :
0 commit comments