From e25b01aa3a105e21d8c6dd742c672ce31997a3e4 Mon Sep 17 00:00:00 2001 From: Bradley Dice Date: Thu, 7 May 2020 17:07:39 -0400 Subject: [PATCH 1/6] Added examples to DataFrame.from_records. --- pandas/core/frame.py | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 4e86b3710a1bd..691e6c1a83d2f 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -1656,6 +1656,47 @@ def from_records( Returns ------- DataFrame + + See Also + -------- + DataFrame.from_dict : DataFrame from dict of array-like or dicts. + DataFrame : DataFrame object creation using constructor. + + Examples + -------- + Data can be provided as a structured ndarray: + + >>> data = np.array([(3, 'a'), (2, 'b'), (1, 'c'), (0, 'd')], + ... dtype=[('col_1', 'i4'), ('col_2', 'U1')]) + >>> pd.DataFrame.from_records(data) + col_1 col_2 + 0 3 a + 1 2 b + 2 1 c + 3 0 d + + Data can be provided as a list of dictionaries: + + >>> data = [{'col_1': 3, 'col_2': 'a'}, + ... {'col_1': 2, 'col_2': 'b'}, + ... {'col_1': 1, 'col_2': 'c'}, + ... {'col_1': 0, 'col_2': 'd'}] + >>> pd.DataFrame.from_records(data) + col_1 col_2 + 0 3 a + 1 2 b + 2 1 c + 3 0 d + + Data can be provided as a list of tuples with corresponding columns: + + >>> data = [(3, 'a'), (2, 'b'), (1, 'c'), (0, 'd')] + >>> pd.DataFrame.from_records(data, columns=['col_1', 'col_2']) + col_1 col_2 + 0 3 a + 1 2 b + 2 1 c + 3 0 d """ # Make a copy of the input columns so we can modify it if columns is not None: From 015d0a7c718aae0d26d99fc8379772e4f7bc7bd3 Mon Sep 17 00:00:00 2001 From: Bradley Dice Date: Thu, 7 May 2020 17:21:21 -0400 Subject: [PATCH 2/6] Clarify allowable inputs to from_records. --- pandas/core/frame.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 691e6c1a83d2f..a327b13b35449 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -1234,7 +1234,7 @@ def from_dict(cls, data, orient="columns", dtype=None, columns=None) -> "DataFra See Also -------- DataFrame.from_records : DataFrame from ndarray (structured - dtype), list of tuples, dict, or DataFrame. + dtype), sequence of tuples, sequence of dicts, or DataFrame. DataFrame : DataFrame object creation using constructor. Examples @@ -1633,9 +1633,13 @@ def from_records( """ Convert structured or record ndarray to DataFrame. + Creates a DataFrame object from a structured ndarray, sequence of + tuples, sequence of dicts, or DataFrame. + Parameters ---------- - data : ndarray (structured dtype), list of tuples, dict, or DataFrame + data : ndarray (structured dtype), sequence of tuples, sequence of dicts, or DataFrame + Structured input data. index : str, list of fields, array-like Field of array to use as the index, alternately a specific set of input labels to use. @@ -1675,7 +1679,7 @@ def from_records( 2 1 c 3 0 d - Data can be provided as a list of dictionaries: + Data can be provided as a list of dicts: >>> data = [{'col_1': 3, 'col_2': 'a'}, ... {'col_1': 2, 'col_2': 'b'}, From e54be8788bfc05c8b7c145146be21effec034212 Mon Sep 17 00:00:00 2001 From: Bradley Dice Date: Thu, 7 May 2020 17:43:21 -0400 Subject: [PATCH 3/6] Allow long type in flake8. --- 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 a327b13b35449..9f107bc9a2b5b 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -1701,7 +1701,7 @@ def from_records( 1 2 b 2 1 c 3 0 d - """ + """ # noqa: E501 # Make a copy of the input columns so we can modify it if columns is not None: columns = ensure_index(columns) From 30b8fced6daa563aab06ff2d525cb98b0d4ac0b1 Mon Sep 17 00:00:00 2001 From: Bradley Dice Date: Fri, 8 May 2020 07:58:56 -0500 Subject: [PATCH 4/6] Simplify wording. Replaced "sequence of tuples, sequence of dicts" with "sequence of tuples or dicts." --- 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 9f107bc9a2b5b..6b71421fae569 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -1234,7 +1234,7 @@ def from_dict(cls, data, orient="columns", dtype=None, columns=None) -> "DataFra See Also -------- DataFrame.from_records : DataFrame from ndarray (structured - dtype), sequence of tuples, sequence of dicts, or DataFrame. + dtype), sequence of tuples or dicts, or DataFrame. DataFrame : DataFrame object creation using constructor. Examples @@ -1634,11 +1634,11 @@ def from_records( Convert structured or record ndarray to DataFrame. Creates a DataFrame object from a structured ndarray, sequence of - tuples, sequence of dicts, or DataFrame. + tuples or dicts, or DataFrame. Parameters ---------- - data : ndarray (structured dtype), sequence of tuples, sequence of dicts, or DataFrame + data : ndarray (structured dtype), sequence of tuples or dicts, or DataFrame Structured input data. index : str, list of fields, array-like Field of array to use as the index, alternately a specific set of @@ -1701,7 +1701,7 @@ def from_records( 1 2 b 2 1 c 3 0 d - """ # noqa: E501 + """ # Make a copy of the input columns so we can modify it if columns is not None: columns = ensure_index(columns) From e2a86f5ffe4bc3eaf3232e877ced62dfc5a102cf Mon Sep 17 00:00:00 2001 From: Bradley Dice Date: Fri, 8 May 2020 08:03:27 -0500 Subject: [PATCH 5/6] Use "structured ndarray" as type description. --- pandas/core/frame.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 6b71421fae569..55c7f26980b63 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -1233,8 +1233,8 @@ def from_dict(cls, data, orient="columns", dtype=None, columns=None) -> "DataFra See Also -------- - DataFrame.from_records : DataFrame from ndarray (structured - dtype), sequence of tuples or dicts, or DataFrame. + DataFrame.from_records : DataFrame from structured ndarray, sequence + of tuples or dicts, or DataFrame. DataFrame : DataFrame object creation using constructor. Examples @@ -1638,7 +1638,7 @@ def from_records( Parameters ---------- - data : ndarray (structured dtype), sequence of tuples or dicts, or DataFrame + data : structured ndarray, sequence of tuples or dicts, or DataFrame Structured input data. index : str, list of fields, array-like Field of array to use as the index, alternately a specific set of From 6b22c254c180f2011706490bdd308afbef2fc987 Mon Sep 17 00:00:00 2001 From: Bradley Dice Date: Fri, 8 May 2020 08:04:24 -0500 Subject: [PATCH 6/6] Fix tabs/spaces from editor misconfiguration. --- 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 55c7f26980b63..7171a02fb5aa4 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -1234,7 +1234,7 @@ def from_dict(cls, data, orient="columns", dtype=None, columns=None) -> "DataFra See Also -------- DataFrame.from_records : DataFrame from structured ndarray, sequence - of tuples or dicts, or DataFrame. + of tuples or dicts, or DataFrame. DataFrame : DataFrame object creation using constructor. Examples