为何Pandas将我的Numpy

为何Pandas将我的Numpy

本文介绍了为何Pandas将我的Numpy Float32强制为Float64?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么熊猫在这段代码中将我的numpy float32强制为float64:

Why does Pandas coerce my numpy float32 to float64 in this piece of code:

>>> import pandas as pd
>>> import numpy as np
>>> df = pd.DataFrame([[1, 2, 'a'], [3, 4, 'b']], dtype=np.float32)
>>> A = df.ix[:, 0:1].values
>>> df.ix[:, 0:1] = A
>>> df[0].dtype
dtype('float64')

这个行为对我来说似乎很奇怪,不知道这是否是一个错误.我使用的是Pandas版本0.17.1(更新的PyPI版本),并且我注意到最近已解决了一些强制性错误,请参见 https://github.com/pydata/pandas/issues/11847 .我没有用更新的GitHub管理员尝试过这段代码.

The behavior seems so odd to me that wonder if it is a bug. I am on Pandas version 0.17.1 (updated PyPI version) and I note there has been coercing bugs recently addressed, see https://github.com/pydata/pandas/issues/11847 . I haven't tried the piece of code with an updated GitHub master.

这是一个错误还是我误解了熊猫的某些功能"?如果它是一项功能,那么我该如何解决?

Is it a bug or do I misunderstand some "feature" in Pandas? If it is a feature, then how do I get around it?

(强制性问题与我最近问过的有关Pandas任务执行情况的问题有关:)

(The coercing problem relates to a question I recently asked about the performance of Pandas assignments: Assignment of Pandas DataFrame with float32 and float64 slow)

推荐答案

我认为值得将此作为GitHub问题发布.行为肯定是不一致的.

I think it is worth posting this as a GitHub issue. The behavior is certainly inconsistent.

代码根据DataFrame是否为混合类型而采用不同的分支().

The code takes a different branch based on whether the DataFrame is mixed-type or not (source).

  • 在混合类型的情况下,将ndarray转换为float64数字的Python列表,然后不考虑DataFrame的dtypes信息而将其转换回float64 ndarray( function may_convert_objects()).

在非混合类型的情况下,DataFrame内容几乎可以直接更新(),然后DataFrame保持其float32 dtypes.

In the non-mixed-type case the DataFrame content is updated pretty much directly (source) and the DataFrame keeps its float32 dtypes.

这篇关于为何Pandas将我的Numpy Float32强制为Float64?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-03 20:28