From 36502e9f79cccbc1cbf490d5cae22be77f476851 Mon Sep 17 00:00:00 2001 From: DriesS Date: Thu, 1 Apr 2021 09:15:11 +0200 Subject: [PATCH] DOC: Clarify DataFrame column argument in API documentation (#40658) --- pandas/core/frame.py | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index bc9c7195b69f2..b22fcbd9229e7 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -472,8 +472,9 @@ class DataFrame(NDFrame, OpsMixin): Index to use for resulting frame. Will default to RangeIndex if no indexing information part of input data and no index provided. columns : Index or array-like - Column labels to use for resulting frame. Will default to - RangeIndex (0, 1, 2, ..., n) if no column labels are provided. + Column labels to use for resulting frame when data does not have them, + defaulting to RangeIndex(0, 1, 2, ..., n). If data contains column labels, + will perform column selection instead. dtype : dtype, default None Data type to force. Only a single dtype is allowed. If None, infer. copy : bool or None, default None @@ -527,6 +528,18 @@ class DataFrame(NDFrame, OpsMixin): 1 4 5 6 2 7 8 9 + Constructing DataFrame from a numpy ndarray that has labeled columns: + + >>> data = np.array([(1, 2, 3), (4, 5, 6), (7, 8, 9)], + ... dtype=[("a", "i4"), ("b", "i4"), ("c", "i4")]) + >>> df3 = pd.DataFrame(data, columns=['c', 'a']) + ... + >>> df3 + c a + 0 3 1 + 1 6 4 + 2 9 7 + Constructing DataFrame from dataclass: >>> from dataclasses import make_dataclass