@@ -1209,20 +1209,58 @@ 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 record array.
1213
+
1214
+ Index will be put in the 'index' field of the record array if requested.
1214
1215
1215
1216
Parameters
1216
1217
----------
1217
1218
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.
1219
1220
convert_datetime64 : boolean, default True
1220
1221
Whether to convert the index to datetime.datetime if it is a
1221
- DatetimeIndex
1222
+ DatetimeIndex.
1222
1223
1223
1224
Returns
1224
1225
-------
1225
1226
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')])
1226
1264
"""
1227
1265
if index :
1228
1266
if is_datetime64_any_dtype (self .index ) and convert_datetime64 :
0 commit comments