diff --git a/ci/doctests.sh b/ci/doctests.sh index 04b3e14a7120a..fee33a0f93f40 100755 --- a/ci/doctests.sh +++ b/ci/doctests.sh @@ -21,7 +21,7 @@ if [ "$DOCTEST" ]; then # DataFrame / Series docstrings pytest --doctest-modules -v pandas/core/frame.py \ - -k"-assign -axes -combine -isin -itertuples -join -nlargest -nsmallest -nunique -pivot_table -quantile -query -reindex -reindex_axis -replace -round -set_index -stack -to_dict -to_records -to_stata -transform" + -k"-assign -axes -combine -isin -itertuples -join -nlargest -nsmallest -nunique -pivot_table -quantile -query -reindex -reindex_axis -replace -round -set_index -stack -to_dict -to_stata -transform" if [ $? -ne "0" ]; then RET=1 diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 7a10fb1023806..409a722017e72 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -1337,14 +1337,15 @@ def to_records(self, index=True, convert_datetime64=None): """ Convert DataFrame to a NumPy record array. - Index will be put in the 'index' field of the record array if + Index will be included as the first field of the record array if requested. Parameters ---------- - index : boolean, default True - Include index in resulting record array, stored in 'index' field. - convert_datetime64 : boolean, default None + index : bool, default True + Include index in resulting record array, stored in 'index' + field or using the index label, if set. + convert_datetime64 : bool, default None .. deprecated:: 0.23.0 Whether to convert the index to datetime.datetime if it is a @@ -1352,7 +1353,9 @@ def to_records(self, index=True, convert_datetime64=None): Returns ------- - y : numpy.recarray + numpy.recarray + NumPy ndarray with the DataFrame labels as fields and each row + of the DataFrame as entries. See Also -------- @@ -1374,31 +1377,20 @@ def to_records(self, index=True, convert_datetime64=None): rec.array([('a', 1, 0.5 ), ('b', 2, 0.75)], dtype=[('index', 'O'), ('A', '>> df.index = df.index.rename("I") + >>> df.to_records() + rec.array([('a', 1, 0.5 ), ('b', 2, 0.75)], + dtype=[('I', 'O'), ('A', '>> df.to_records(index=False) rec.array([(1, 0.5 ), (2, 0.75)], dtype=[('A', '>> df.index = pd.date_range('2018-01-01 09:00', periods=2, freq='min') - >>> df - A B - 2018-01-01 09:00:00 1 0.50 - 2018-01-01 09:01:00 2 0.75 - >>> df.to_records() - rec.array([(datetime.datetime(2018, 1, 1, 9, 0), 1, 0.5 ), - (datetime.datetime(2018, 1, 1, 9, 1), 2, 0.75)], - dtype=[('index', 'O'), ('A', '>> df.to_records(convert_datetime64=False) - rec.array([('2018-01-01T09:00:00.000000000', 1, 0.5 ), - ('2018-01-01T09:01:00.000000000', 2, 0.75)], - dtype=[('index', '