pandas AttributeError: 'unicode' 对象没有属性 'view'

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26523324/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 22:36:24  来源:igfitidea点击:

pandas AttributeError: 'unicode' object has no attribute 'view'

pythonpandas

提问by HaPsantran

This is a killer problem that probably has a simple solution for a pandas newbie like me:

这是一个杀手问题,对于像我这样的Pandas新手来说可能有一个简单的解决方案:

I'm trying to replace one record of pandas DataFrame (df) with the latest version of that label (found in a separate DataFrame (latest_version).

我正在尝试用该标签的最新版本(在单独的 DataFrame (latest_version) 中找到)替换 pandas DataFrame (df) 的一条记录。

df.ix[label] = latest_version.ix[label]

The error:

错误:

AttributeError: 'unicode' object has no attribute 'view'

df itself is large and complex (and proprietary) so I'd like to avoid posting it if I can; I'm hoping there's something easy I'm missing but I can't figure it out.

df 本身又大又复杂(并且是专有的),所以我想尽可能避免发布它;我希望我错过了一些简单的东西,但我无法弄清楚。

EDIT: output of df.info() and latest_version.info()

编辑:df.info() 和 latest_version.info() 的输出

ipdb> df.info()
<class 'pandas.core.frame.DataFrame'>
Index: 7 entries, A to G
Data columns (total 73 columns):
Column 0   7 non-null object
Column 1   7 non-null object
Column 2   7 non-null object
Column 3   7 non-null object
Column 4   7 non-null object
Column 5   7 non-null float64
Column 6   1 non-null object
Column 7   7 non-null object
Column 8   7 non-null object
Column 9   6 non-null datetime64[ns]
Column 10  0 non-null object
Column 11  0 non-null object
Column 12  5 non-null object
Column 13  0 non-null object
Column 14  0 non-null object
Column 15  6 non-null datetime64[ns]
Column 16  0 non-null object
Column 17  0 non-null object
Column 18  0 non-null object
Column 19  0 non-null object
Column 20  0 non-null object
Column 21  0 non-null object
Column 22  0 non-null object
Column 23  0 non-null object
Column 24  0 non-null object
Column 25  0 non-null object
Column 26  0 non-null object
Column 27  0 non-null object
Column 28  0 non-null object
Column 29  0 non-null object
Column 30  0 non-null object
Column 31  0 non-null object
Column 32  0 non-null object
Column 33  0 non-null object
Column 34  0 non-null object
Column 35  0 non-null object
Column 36  0 non-null object
Column 37  4 non-null object
Column 38  6 non-null object
Column 39  4 non-null object
Column 40  0 non-null object
Column 41  0 non-null object
Column 42  0 non-null object
Column 43  6 non-null object
Column 44  0 non-null object
Column 45  6 non-null object
Column 46  0 non-null object
Column 47  4 non-null object
Column 48  0 non-null object
Column 49  4 non-null object
Column 50  0 non-null object
Column 51  0 non-null object
Column 52  0 non-null object
Column 53  0 non-null object
Column 54  0 non-null object
Column 55  0 non-null object
Column 56  0 non-null object
Column 57  0 non-null object
Column 58  0 non-null object
Column 59  0 non-null object
Column 60  0 non-null object
Column 61  0 non-null object
Column 62  0 non-null object
Column 63  0 non-null object
Column 64  0 non-null object
Column 65  0 non-null object
Column 66  0 non-null object
Column 67  0 non-null object
Column 68  0 non-null object
Column 69  0 non-null object
Column 70  0 non-null object
Column 71  0 non-null object
Column 72  0 non-null object
dtypes: datetime64[ns](2), float64(1), object(70)ipdb> 

ipdb> latest_version.info()
<class 'pandas.core.frame.DataFrame'>
Index: 4 entries, A to D
Data columns (total 73 columns):
Column 0   4 non-null object
Column 1   4 non-null object
Column 2   4 non-null object
Column 3   4 non-null object
Column 4   4 non-null object
Column 5   4 non-null int64
Column 6   4 non-null object
Column 7   4 non-null object
Column 8   4 non-null object
Column 9   4 non-null object
Column 10  4 non-null object
Column 11  4 non-null object
Column 12  4 non-null object
Column 13  4 non-null object
Column 14  4 non-null object
Column 15  4 non-null object
Column 16  3 non-null object
Column 17  4 non-null object
Column 18  4 non-null object
Column 19  4 non-null object
Column 20  3 non-null object
Column 21  3 non-null object
Column 22  4 non-null object
Column 23  4 non-null object
Column 24  4 non-null object
Column 25  4 non-null object
Column 26  4 non-null object
Column 27  4 non-null object
Column 28  4 non-null object
Column 29  4 non-null object
Column 30  4 non-null object
Column 31  4 non-null object
Column 32  4 non-null object
Column 33  4 non-null object
Column 34  4 non-null object
Column 35  4 non-null object
Column 36  4 non-null object
Column 37  4 non-null object
Column 38  4 non-null object
Column 39  4 non-null object
Column 40  4 non-null object
Column 41  4 non-null object
Column 42  4 non-null object
Column 43  4 non-null object
Column 44  4 non-null object
Column 45  4 non-null float64
Column 46  4 non-null object
Column 47  4 non-null object
Column 48  4 non-null object
Column 49  4 non-null object
Column 50  4 non-null object
Column 51  4 non-null object
Column 52  4 non-null object
Column 53  4 non-null object
Column 54  4 non-null object
Column 55  4 non-null object
Column 56  1 non-null object
Column 57  1 non-null object
Column 58  4 non-null object
Column 59  4 non-null object
Column 60  4 non-null object
Column 61  4 non-null object
Column 62  4 non-null object
Column 63  4 non-null object
Column 64  4 non-null object
Column 65  4 non-null object
Column 66  4 non-null object
Column 67  4 non-null object
Column 68  4 non-null object
Column 69  4 non-null object
Column 70  4 non-null object
Column 71  4 non-null object
Column 72  4 non-null object
dtypes: float64(1), int64(1), object(71)ipdb> 

Further edit (in response to Ed): Here are the tables with just the columns that have different types:

进一步编辑(回应 Ed):以下是仅包含不同类型列的表格:

ipdb> latest_version.ix[:,[5,9,15]]
                                    line_number  entry_date entry_ref_a
unique_index                                                           
NEW/AAAAAAAAAAAAAAAAAAA                       0  2014-12-30  2015-01-14
NEW/AAAAAAAAAAAAAAAAAAB                       1  2014-12-30            
NEW/AAAAAAAAAAAAAAAAAAC                       2  2014-12-30            
ipdb>/df.ix[:,[5,9,15]]
                                    line_number           entry_date  \
unique_index                                                           
OLD/204442                                    0  1419897600000000000   
OLD/343278                                    1  1419897600000000000   
OLD/359628                                    2  1419897600000000000   
NEW/AAAAAAAAAAAAAAAAAAA                       0           2014-12-30   

                                            entry_ref_a  
unique_index                                             
OLD/204442                          1421193600000000000  
OLD/343278                          1421193600000000000  
OLD/359628                          1422230400000000000  
NEW/AAAAAAAAAAAAAAAAAAA                      2015-01-14  

Definitely lends credence to the idea that there's a type mismatch issue here...

绝对证明这里存在类型不匹配问题的想法......

采纳答案by EdChum

So your problem here seems to be that you had a mismatch on the dtypes between the 2 dfs you were trying to assign to and from:

因此,您的问题似乎是您尝试分配的 2 个 dfs 之间的 dtypes 不匹配:

df dtypes: datetime64[ns](2), float64(1), object(70) 

whilst

同时

latest_version is :dtypes: float64(1), int64(1), object(71)

From the output we can see that that columns that clash some are datetimes, whilst they are int64's in the corresponding column in the other df.

从输出中我们可以看到,有些冲突的列是日期时间,而它们在另一个 df 的相应列中是 int64。

You can convert the ill-formed columns to datetime by doing:

您可以通过执行以下操作将格式错误的列转换为日期时间:

df['entry_date'] = pd.to_datetime(df['entry_date')

and likewise for entry_ref_a

同样对于 entry_ref_a