@@ -1209,20 +1209,68 @@ def from_records(cls, data, index=None, exclude=None, columns=None,
1209
1209
1210
1210
def to_records (self , index = True , convert_datetime64 = True ):
1211
1211
"""
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.
1214
1216
1215
1217
Parameters
1216
1218
----------
1217
1219
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.
1219
1221
convert_datetime64 : boolean, default True
1220
1222
Whether to convert the index to datetime.datetime if it is a
1221
- DatetimeIndex
1223
+ DatetimeIndex.
1222
1224
1223
1225
Returns
1224
1226
-------
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')])
1226
1274
"""
1227
1275
if index :
1228
1276
if is_datetime64_any_dtype (self .index ) and convert_datetime64 :
0 commit comments