From bcf416c78ad64cca52f3a98f3bf4217aa3f39ad9 Mon Sep 17 00:00:00 2001 From: Samuel Date: Sat, 10 Mar 2018 15:23:58 +0000 Subject: [PATCH 1/8] Update to_records docstring. - Minor changes (missing dots, newlines) to make tests pass. - More examples. --- pandas/core/frame.py | 46 ++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 42 insertions(+), 4 deletions(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index a66d00fff9714..73cbe1aef6f05 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -1209,20 +1209,58 @@ def from_records(cls, data, index=None, exclude=None, columns=None, def to_records(self, index=True, convert_datetime64=True): """ - Convert DataFrame to record array. Index will be put in the - 'index' field of the record array if requested + Convert DataFrame to record array. + + Index will be put in the 'index' field of the record array if requested. Parameters ---------- index : boolean, default True - Include index in resulting record array, stored in 'index' field + Include index in resulting record array, stored in 'index' field. convert_datetime64 : boolean, default True Whether to convert the index to datetime.datetime if it is a - DatetimeIndex + DatetimeIndex. Returns ------- y : recarray + + See Also + -------- + DataFrame.from_records: convert structured or record ndarray to DataFrame. + + Examples + -------- + >>> df = pd.DataFrame({'col1': [1, 2], 'col2': [0.5, 0.75]}, index=['a', 'b']) + >>> df + col1 col2 + a 1 0.50 + b 2 0.75 + >>> df.to_records() + rec.array([('a', 1, 0.5 ), ('b', 2, 0.75)], + dtype=[('index', 'O'), ('col1', '>> df.to_records(index=False) + rec.array([(1, 0.5 ), (2, 0.75)], + dtype=[('col1', '>> df.index = pd.date_range('2018-01-01 09:00', periods=2, freq='min') + >>> df + col1 col2 + 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'), ('col1', '>> 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: Sat, 10 Mar 2018 15:28:39 +0000 Subject: [PATCH 2/8] Fix html docs. Missing newlines. --- pandas/core/frame.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 73cbe1aef6f05..abe88c3057ffb 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -1231,6 +1231,7 @@ def to_records(self, index=True, convert_datetime64=True): Examples -------- + >>> df = pd.DataFrame({'col1': [1, 2], 'col2': [0.5, 0.75]}, index=['a', 'b']) >>> df col1 col2 @@ -1241,11 +1242,13 @@ def to_records(self, index=True, convert_datetime64=True): dtype=[('index', 'O'), ('col1', '>> df.to_records(index=False) rec.array([(1, 0.5 ), (2, 0.75)], dtype=[('col1', '>> df.index = pd.date_range('2018-01-01 09:00', periods=2, freq='min') >>> df col1 col2 @@ -1257,6 +1260,7 @@ def to_records(self, index=True, convert_datetime64=True): dtype=[('index', 'O'), ('col1', '>> 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)], From b03dda7f6beb319de334796e94eee5bcb73a7ec0 Mon Sep 17 00:00:00 2001 From: Samuel Date: Sat, 10 Mar 2018 15:30:14 +0000 Subject: [PATCH 3/8] Reword datetime type information. --- pandas/core/frame.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index abe88c3057ffb..9c138cb616631 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -1247,7 +1247,7 @@ def to_records(self, index=True, convert_datetime64=True): rec.array([(1, 0.5 ), (2, 0.75)], dtype=[('col1', '>> df.index = pd.date_range('2018-01-01 09:00', periods=2, freq='min') >>> df @@ -1259,7 +1259,7 @@ def to_records(self, index=True, convert_datetime64=True): (datetime.datetime(2018, 1, 1, 9, 1), 2, 0.75)], dtype=[('index', 'O'), ('col1', '>> df.to_records(convert_datetime64=False) rec.array([('2018-01-01T09:00:00.000000000', 1, 0.5 ), From e7b4cf2883e0e84e742b27a09e7dfff7e152fa77 Mon Sep 17 00:00:00 2001 From: Samuel Date: Sat, 10 Mar 2018 15:53:19 +0000 Subject: [PATCH 4/8] flake8 errors --- pandas/core/frame.py | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 9c138cb616631..ff9c977844393 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -1211,7 +1211,8 @@ def to_records(self, index=True, convert_datetime64=True): """ Convert DataFrame to record array. - Index will be put in the 'index' field of the record array if requested. + Index will be put in the 'index' field of the record array if + requested. Parameters ---------- @@ -1227,44 +1228,50 @@ def to_records(self, index=True, convert_datetime64=True): See Also -------- - DataFrame.from_records: convert structured or record ndarray to DataFrame. + DataFrame.from_records: convert structured or record ndarray + to DataFrame. + numpy.recarray: ndarray that allows field access using + attributes, analogous to typed (typed) columns in a + spreadsheet. Examples -------- - >>> df = pd.DataFrame({'col1': [1, 2], 'col2': [0.5, 0.75]}, index=['a', 'b']) + >>> df = pd.DataFrame({'A': [1, 2], 'B': [0.5, 0.75]}, + ... index=['a', 'b']) >>> df - col1 col2 + A B a 1 0.50 b 2 0.75 >>> df.to_records() rec.array([('a', 1, 0.5 ), ('b', 2, 0.75)], - dtype=[('index', 'O'), ('col1', '>> df.to_records(index=False) rec.array([(1, 0.5 ), (2, 0.75)], - dtype=[('col1', '>> df.index = pd.date_range('2018-01-01 09:00', periods=2, freq='min') >>> df - col1 col2 + 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'), ('col1', '>> 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: Sat, 10 Mar 2018 18:14:51 +0000 Subject: [PATCH 5/8] Fix typo (duplicated type) --- 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 ff9c977844393..07d5e681ea2c2 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -1231,7 +1231,7 @@ def to_records(self, index=True, convert_datetime64=True): DataFrame.from_records: convert structured or record ndarray to DataFrame. numpy.recarray: ndarray that allows field access using - attributes, analogous to typed (typed) columns in a + attributes, analogous to typed columns in a spreadsheet. Examples From 2d0a3bb9efbdc86fcb47e180f0501623f831aa55 Mon Sep 17 00:00:00 2001 From: Samuel Date: Sat, 10 Mar 2018 18:18:19 +0000 Subject: [PATCH 6/8] Remove unwanted blank line after Examples. --- pandas/core/frame.py | 1 - 1 file changed, 1 deletion(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 07d5e681ea2c2..d5a0568c9a53b 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -1236,7 +1236,6 @@ def to_records(self, index=True, convert_datetime64=True): Examples -------- - >>> df = pd.DataFrame({'A': [1, 2], 'B': [0.5, 0.75]}, ... index=['a', 'b']) >>> df From 5f6e1c5057c8decf2056bc6fc5c425388875c8b4 Mon Sep 17 00:00:00 2001 From: Samuel Date: Sat, 10 Mar 2018 18:22:04 +0000 Subject: [PATCH 7/8] Fix doctests. ``` (pandas_dev) sinayoks@landade:~/dev/pandas/ $ pytest --doctest-modules pandas/core/frame.py -k to_record ========================================================================================== test session starts ========================================================================================== platform darwin -- Python 3.6.4, pytest-3.4.2, py-1.5.2, pluggy-0.6.0 rootdir: /Users/sinayoks/dev/pandas, inifile: setup.cfg plugins: xdist-1.22.1, forked-0.2, cov-2.5.1 collected 43 items pandas/core/frame.py . [100%] ========================================================================================== 42 tests deselected ========================================================================================== ``` --- pandas/core/frame.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index d5a0568c9a53b..362f025ba5bd8 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -1239,9 +1239,9 @@ def to_records(self, index=True, convert_datetime64=True): >>> df = pd.DataFrame({'A': [1, 2], 'B': [0.5, 0.75]}, ... index=['a', 'b']) >>> df - A B - a 1 0.50 - b 2 0.75 + A B + a 1 0.50 + b 2 0.75 >>> df.to_records() rec.array([('a', 1, 0.5 ), ('b', 2, 0.75)], dtype=[('index', 'O'), ('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 + 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)], From b1d0b092ffa72941bbf0c6cbf92232f3238ece60 Mon Sep 17 00:00:00 2001 From: Samuel Date: Sun, 11 Mar 2018 09:10:14 +0000 Subject: [PATCH 8/8] Few more changes --- pandas/core/frame.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 362f025ba5bd8..7b112c5be6e8d 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -1209,7 +1209,7 @@ def from_records(cls, data, index=None, exclude=None, columns=None, def to_records(self, index=True, convert_datetime64=True): """ - Convert DataFrame to record array. + Convert DataFrame to a NumPy record array. Index will be put in the 'index' field of the record array if requested. @@ -1224,7 +1224,7 @@ def to_records(self, index=True, convert_datetime64=True): Returns ------- - y : recarray + y : numpy.recarray See Also --------