From 7e05b136945debf56edfc8867dd296aafbea2c04 Mon Sep 17 00:00:00 2001 From: Mathew Topper Date: Sun, 19 Aug 2018 11:15:43 +0100 Subject: [PATCH 1/8] Updates examples of DataFrame.to_records for new behaviour. --- pandas/core/frame.py | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 7a10fb1023806..918bccd074092 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -1380,7 +1380,8 @@ def to_records(self, index=True, convert_datetime64=None): 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 @@ -1388,17 +1389,24 @@ def to_records(self, index=True, convert_datetime64=None): 2018-01-01 09:00:00 1 0.50 2018-01-01 09:01:00 2 0.75 >>> df.to_records() + rec.array([('2018-01-01T09:00:00.000000000', 1, 0.5 ), + ('2018-01-01T09:01:00.000000000', 2, 0.75)], + dtype=[('index', '>> df.to_records(convert_datetime64=True) 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', ' Date: Sun, 19 Aug 2018 11:16:12 +0100 Subject: [PATCH 2/8] Enable doctests for DataFrame.to_records --- ci/doctests.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 From ff1a31af54a23a9f6a0b3dc71eaa6fe8255b2433 Mon Sep 17 00:00:00 2001 From: Mathew Topper Date: Sun, 19 Aug 2018 11:22:34 +0100 Subject: [PATCH 3/8] Fix whitespace --- pandas/core/frame.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 918bccd074092..20acf54b7da5b 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -1394,19 +1394,19 @@ def to_records(self, index=True, convert_datetime64=None): dtype=[('index', '>> df.to_records(convert_datetime64=True) 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', ' Date: Sun, 19 Aug 2018 21:45:16 +0100 Subject: [PATCH 4/8] Fix types in parameter definitions and add description for returns in DataFrame.to_records --- pandas/core/frame.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 20acf54b7da5b..66d4c7aabad9c 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -1342,9 +1342,9 @@ def to_records(self, index=True, convert_datetime64=None): Parameters ---------- - index : boolean, default True + index : bool, default True Include index in resulting record array, stored in 'index' field. - convert_datetime64 : boolean, default None + convert_datetime64 : bool, default None .. deprecated:: 0.23.0 Whether to convert the index to datetime.datetime if it is a @@ -1352,7 +1352,9 @@ def to_records(self, index=True, convert_datetime64=None): Returns ------- - y : numpy.recarray + numpy.recarray + NumPy ndarray with the Dataframe labels as attributes and each row + of the Dataframe as an entry in the array. See Also -------- From 58c1d8cea530c828bc7169f47d2f21633c6090e1 Mon Sep 17 00:00:00 2001 From: Mathew Topper Date: Sun, 19 Aug 2018 21:46:11 +0100 Subject: [PATCH 5/8] Remove examples of using convert_datetime64 parameter in DataFrame.to_records --- pandas/core/frame.py | 28 ---------------------------- 1 file changed, 28 deletions(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 66d4c7aabad9c..c5cb283d15db2 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -1381,34 +1381,6 @@ def to_records(self, index=True, convert_datetime64=None): >>> 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([('2018-01-01T09:00:00.000000000', 1, 0.5 ), - ('2018-01-01T09:01:00.000000000', 2, 0.75)], - dtype=[('index', '>> df.to_records(convert_datetime64=True) - 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', ' Date: Sun, 19 Aug 2018 21:46:50 +0100 Subject: [PATCH 6/8] Add example of converting a named index in DataFrame.to_records() --- pandas/core/frame.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index c5cb283d15db2..ad4c1ee58f0e4 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -1376,6 +1376,15 @@ 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.rename("I", inplace=True) + >>> df.to_records() + rec.array([('a', 1, 0.5 ), ('b', 2, 0.75)], + dtype=[('I', 'O'), ('A', '>> df.to_records(index=False) From 62c8dd507879bde677000b55cc47b9b56ffc8124 Mon Sep 17 00:00:00 2001 From: Mathew Topper Date: Mon, 20 Aug 2018 10:14:27 +0100 Subject: [PATCH 7/8] Update frame.py * Fixed incorrect case in DataFrame * Changed 'attributes' to 'fields' when describing recarray features * Improved description of how the index is set in the recarray --- pandas/core/frame.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index ad4c1ee58f0e4..faa9890930e5c 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -1337,13 +1337,14 @@ 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 : bool, default True - Include index in resulting record array, stored in 'index' field. + 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 @@ -1353,8 +1354,8 @@ def to_records(self, index=True, convert_datetime64=None): Returns ------- numpy.recarray - NumPy ndarray with the Dataframe labels as attributes and each row - of the Dataframe as an entry in the array. + NumPy ndarray with the DataFrame labels as fields and each row + of the DataFrame as entries. See Also -------- @@ -1376,9 +1377,9 @@ 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.rename("I", inplace=True) >>> df.to_records() From 6fd1f4f8aee5df98f52f62ef9ce37a467a958126 Mon Sep 17 00:00:00 2001 From: Mathew Topper Date: Mon, 20 Aug 2018 11:33:44 +0100 Subject: [PATCH 8/8] Remove use of inplace keyword arg --- pandas/core/frame.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index faa9890930e5c..409a722017e72 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -1381,7 +1381,7 @@ def to_records(self, index=True, convert_datetime64=None): is set to 'index'. If the index has a label then this is used as the field name: - >>> df.index.rename("I", inplace=True) + >>> df.index = df.index.rename("I") >>> df.to_records() rec.array([('a', 1, 0.5 ), ('b', 2, 0.75)], dtype=[('I', 'O'), ('A', '