2016-01-21 29 views
4

我有以下數據框:爲什麼在大熊貓數據幀列應用改變D型

import pandas as pd 
import numpy as np 
df = pd.DataFrame(dict(A = np.arange(3), 
         B = np.random.randn(3), 
         C = ['foo','bar','bah'], 
         D = pd.Timestamp('20130101'))) 

print(df) 

    A   B C   D 
0 0 -1.087180 foo 2013-01-01 
1 1 -1.343424 bar 2013-01-01 
2 2 -0.193371 bah 2013-01-01 

dtypes爲列:

print(df.dtypes) 
A    int32 
B   float64 
C   object 
D datetime64[ns] 
dtype: object 

但使用apply後,他們都改變對象:

print(df.apply(lambda x: x.dtype)) 
A object 
B object 
C object 
D object 
dtype: object 

爲什麼dtypes被脅迫對象?我認爲在apply只應該考慮列。

pandas 0.17.1
python 3.4.3

回答

4

您可以使用參數reduce=False和更多信息here

print (df.apply(lambda x: x.dtype, reduce=False)) 

A    int32 
B   float64 
C   object 
D datetime64[ns] 
dtype: object 
+0

謝謝,沒有找到,而搜索。 –