Skip to content

Commit fbdae2d

Browse files
VincentLajorisvandenbossche
authored andcommitted
DOC: Update to docstring of DataFrame(dtype) (#14764) (#16487)
* Adding some more documentation on dataframe with regards to dtype * Making example for creating dataframe from np matrix easier
1 parent 92d0799 commit fbdae2d

File tree

1 file changed

+36
-6
lines changed

1 file changed

+36
-6
lines changed

pandas/core/frame.py

+36-6
Original file line numberDiff line numberDiff line change
@@ -241,17 +241,47 @@ class DataFrame(NDFrame):
241241
Column labels to use for resulting frame. Will default to
242242
np.arange(n) if no column labels are provided
243243
dtype : dtype, default None
244-
Data type to force, otherwise infer
244+
Data type to force. Only a single dtype is allowed. If None, infer
245245
copy : boolean, default False
246246
Copy data from inputs. Only affects DataFrame / 2d ndarray input
247247
248248
Examples
249249
--------
250-
>>> d = {'col1': ts1, 'col2': ts2}
251-
>>> df = DataFrame(data=d, index=index)
252-
>>> df2 = DataFrame(np.random.randn(10, 5))
253-
>>> df3 = DataFrame(np.random.randn(10, 5),
254-
... columns=['a', 'b', 'c', 'd', 'e'])
250+
Constructing DataFrame from a dictionary.
251+
252+
>>> d = {'col1': [1, 2], 'col2': [3, 4]}
253+
>>> df = pd.DataFrame(data=d)
254+
>>> df
255+
col1 col2
256+
0 1 3
257+
1 2 4
258+
259+
Notice that the inferred dtype is int64.
260+
261+
>>> df.dtypes
262+
col1 int64
263+
col2 int64
264+
dtype: object
265+
266+
To enforce a single dtype:
267+
268+
>>> df = pd.DataFrame(data=d, dtype=np.int8)
269+
>>> df.dtypes
270+
col1 int8
271+
col2 int8
272+
dtype: object
273+
274+
Constructing DataFrame from numpy ndarray:
275+
276+
>>> df2 = pd.DataFrame(np.random.randint(low=0, high=10, size=(5, 5)),
277+
... columns=['a', 'b', 'c', 'd', 'e'])
278+
>>> df2
279+
a b c d e
280+
0 2 8 8 3 4
281+
1 4 2 9 0 9
282+
2 1 0 7 8 0
283+
3 5 1 7 1 3
284+
4 6 0 2 4 2
255285
256286
See also
257287
--------

0 commit comments

Comments
 (0)