@@ -105,27 +105,54 @@ Conversion
105
105
106
106
Mixed Conversion
107
107
108
- .. ipython :: python
109
- :okwarning:
108
+ .. code-block :: ipython
110
109
111
- df3[' D' ] = ' 1.'
112
- df3[' E' ] = ' 1'
113
- df3.convert_objects(convert_numeric = True ).dtypes
110
+ In [12]: df3['D'] = '1.'
114
111
115
- # same, but specific dtype conversion
116
- df3[' D' ] = df3[' D' ].astype(' float16' )
117
- df3[' E' ] = df3[' E' ].astype(' int32' )
118
- df3.dtypes
112
+ In [13]: df3['E'] = '1'
113
+
114
+ In [14]: df3.convert_objects(convert_numeric=True).dtypes
115
+ Out[14]:
116
+ A float32
117
+ B float64
118
+ C float64
119
+ D float64
120
+ E int64
121
+ dtype: object
122
+
123
+ # same, but specific dtype conversion
124
+ In [15]: df3['D'] = df3['D'].astype('float16')
125
+
126
+ In [16]: df3['E'] = df3['E'].astype('int32')
127
+
128
+ In [17]: df3.dtypes
129
+ Out[17]:
130
+ A float32
131
+ B float64
132
+ C float64
133
+ D float16
134
+ E int32
135
+ dtype: object
119
136
120
137
Forcing Date coercion (and setting ``NaT `` when not datelike)
121
138
122
- .. ipython :: python
123
- :okwarning:
139
+ .. code-block :: ipython
140
+
141
+ In [18]: import datetime
142
+
143
+ In [19]: s = pd.Series([datetime.datetime(2001, 1, 1, 0, 0), 'foo', 1.0, 1,
144
+ ....: pd.Timestamp('20010104'), '20010105'], dtype='O')
145
+ ....:
124
146
125
- import datetime
126
- s = pd.Series([datetime.datetime(2001 , 1 , 1 , 0 , 0 ), ' foo' , 1.0 , 1 ,
127
- pd.Timestamp(' 20010104' ), ' 20010105' ], dtype = ' O' )
128
- s.convert_objects(convert_dates = ' coerce' )
147
+ In [20]: s.convert_objects(convert_dates='coerce')
148
+ Out[20]:
149
+ 0 2001-01-01
150
+ 1 NaT
151
+ 2 NaT
152
+ 3 NaT
153
+ 4 2001-01-04
154
+ 5 2001-01-05
155
+ dtype: datetime64[ns]
129
156
130
157
Dtype Gotchas
131
158
~~~~~~~~~~~~~
@@ -138,11 +165,22 @@ dtypes, they *WILL* be respected, however (:issue:`2837`)
138
165
139
166
The following will all result in ``int64 `` dtypes
140
167
141
- .. ipython :: python
168
+ .. code-block :: ipython
169
+
170
+ In [21]: pd.DataFrame([1, 2], columns=['a']).dtypes
171
+ Out[21]:
172
+ a int64
173
+ dtype: object
174
+
175
+ In [22]: pd.DataFrame({'a': [1, 2]}).dtypes
176
+ Out[22]:
177
+ a int64
178
+ dtype: object
142
179
143
- pd.DataFrame([1 , 2 ], columns = [' a' ]).dtypes
144
- pd.DataFrame({' a' : [1 , 2 ]}).dtypes
145
- pd.DataFrame({' a' : 1 }, index = range (2 )).dtypes
180
+ In [23]: pd.DataFrame({'a': 1}, index=range(2)).dtypes
181
+ Out[23]:
182
+ a int64
183
+ dtype: object
146
184
147
185
Keep in mind that ``DataFrame(np.array([1,2])) `` **WILL ** result in ``int32 `` on 32-bit platforms!
148
186
@@ -152,28 +190,95 @@ Keep in mind that ``DataFrame(np.array([1,2]))`` **WILL** result in ``int32`` on
152
190
Performing indexing operations on integer type data can easily upcast the data.
153
191
The dtype of the input data will be preserved in cases where ``nans `` are not introduced.
154
192
155
- .. ipython :: python
156
-
157
- dfi = df3.astype(' int32' )
158
- dfi[' D' ] = dfi[' D' ].astype(' int64' )
159
- dfi
160
- dfi.dtypes
161
-
162
- casted = dfi[dfi > 0 ]
163
- casted
164
- casted.dtypes
193
+ .. code-block :: ipython
194
+
195
+ In [24]: dfi = df3.astype('int32')
196
+
197
+ In [25]: dfi['D'] = dfi['D'].astype('int64')
198
+
199
+ In [26]: dfi
200
+ Out[26]:
201
+ A B C D E
202
+ 0 0 0 0 1 1
203
+ 1 -2 0 1 1 1
204
+ 2 -2 0 2 1 1
205
+ 3 0 -1 3 1 1
206
+ 4 1 0 4 1 1
207
+ 5 0 0 5 1 1
208
+ 6 0 -1 6 1 1
209
+ 7 0 0 7 1 1
210
+
211
+ In [27]: dfi.dtypes
212
+ Out[27]:
213
+ A int32
214
+ B int32
215
+ C int32
216
+ D int64
217
+ E int32
218
+ dtype: object
219
+
220
+ In [28]: casted = dfi[dfi > 0]
221
+
222
+ In [29]: casted
223
+ Out[29]:
224
+ A B C D E
225
+ 0 NaN NaN NaN 1 1
226
+ 1 NaN NaN 1.0 1 1
227
+ 2 NaN NaN 2.0 1 1
228
+ 3 NaN NaN 3.0 1 1
229
+ 4 1.0 NaN 4.0 1 1
230
+ 5 NaN NaN 5.0 1 1
231
+ 6 NaN NaN 6.0 1 1
232
+ 7 NaN NaN 7.0 1 1
233
+
234
+ In [30]: casted.dtypes
235
+ Out[30]:
236
+ A float64
237
+ B float64
238
+ C float64
239
+ D int64
240
+ E int32
241
+ dtype: object
165
242
166
243
While float dtypes are unchanged.
167
244
168
- .. ipython :: python
169
-
170
- df4 = df3.copy()
171
- df4[' A' ] = df4[' A' ].astype(' float32' )
172
- df4.dtypes
173
-
174
- casted = df4[df4 > 0 ]
175
- casted
176
- casted.dtypes
245
+ .. code-block :: ipython
246
+
247
+ In [31]: df4 = df3.copy()
248
+
249
+ In [32]: df4['A'] = df4['A'].astype('float32')
250
+
251
+ In [33]: df4.dtypes
252
+ Out[33]:
253
+ A float32
254
+ B float64
255
+ C float64
256
+ D float16
257
+ E int32
258
+ dtype: object
259
+
260
+ In [34]: casted = df4[df4 > 0]
261
+
262
+ In [35]: casted
263
+ Out[35]:
264
+ A B C D E
265
+ 0 NaN NaN NaN 1.0 1
266
+ 1 NaN 0.567020 1.0 1.0 1
267
+ 2 NaN 0.276232 2.0 1.0 1
268
+ 3 NaN NaN 3.0 1.0 1
269
+ 4 1.933792 NaN 4.0 1.0 1
270
+ 5 NaN 0.113648 5.0 1.0 1
271
+ 6 NaN NaN 6.0 1.0 1
272
+ 7 NaN 0.524988 7.0 1.0 1
273
+
274
+ In [36]: casted.dtypes
275
+ Out[36]:
276
+ A float32
277
+ B float64
278
+ C float64
279
+ D float16
280
+ E int32
281
+ dtype: object
177
282
178
283
Datetimes Conversion
179
284
~~~~~~~~~~~~~~~~~~~~
0 commit comments