Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python astype不适用于数据帧_Python_Pandas_Dataframe - Fatal编程技术网

Python astype不适用于数据帧

Python astype不适用于数据帧,python,pandas,dataframe,Python,Pandas,Dataframe,我有一个名为messages的数据框,其中的数据看起来像 message length class hello, Come here 16 A hi, how are you 15 A what is it 10 B maybe tomorrow 14 A 当我这样做的时候 messages.dtypes 它告诉我 class object message o

我有一个名为messages的数据框,其中的数据看起来像

message             length  class
hello, Come here      16     A
hi, how are you       15     A 
what is it            10     B
maybe tomorrow        14     A
当我这样做的时候

messages.dtypes
它告诉我

class      object
message    object
Length      int64
dtype: object
然后我尝试将消息列转换为字符串类型

messages['message'] = messages['message'].astype(str)
print messages.dtypes
它仍然让我明白

class      object
message    object
Length      int64
dtype: object
我做错了什么。为什么不转换成字符串呢

windows 10上的Python 2.7.9版
熊猫版本0.15.2

没有“字符串”数据类型。在熊猫中,字符串存储为对象


在numpy中,可以有字符串数据类型,但它们是固定长度的,所以仍然没有“字符串数据类型”。对于5个字符的字符串有一个数据类型,对于10个字符的字符串有一个数据类型,等等,但是对于“字符串”本身没有数据类型。Pandas使用
对象
作为字符串的数据类型,以便您可以对字符串执行大小更改操作(例如,将其与其他字符串连接)无需使用新的字符串长度重新创建整个列。

@HashRocketSyntax:但请注意,该示例仍然给出了一个具有dtype
object
的结果。您可以使用它,但它不会像OP所发现的那样创建任何dtype
str