python - 为什么将字符串的总和转换为 float

标签 python pandas

设置

考虑以下数据框(注意字符串):

df = pd.DataFrame([['3', '11'], ['0', '2']], columns=list('AB'))
df

enter image description here

df.info()

<class 'pandas.core.frame.DataFrame'>
RangeIndex: 2 entries, 0 to 1
Data columns (total 2 columns):
A    2 non-null object
B    2 non-null object
dtypes: object(2)
memory usage: 104.0+ bytes

问题

我要总结一下。我希望将字符串连接起来。

df.sum()

A     30.0
B    112.0
dtype: float64

看起来好像字符串被连接然后转换为 float 。这有充分的理由吗?这是一个错误吗?任何有启发性的事情都会被投票。

最佳答案

使用良好的旧堆栈跟踪。也通过 Pycharm 了解了一些关于 pdb 的知识。结果发生了以下情况:

1)

cls.sum = _make_stat_function(
            'sum', name, name2, axis_descr,
            'Return the sum of the values for the requested axis',
            nanops.nansum)

让我们看看_make_stat_function

2)

def _make_stat_function(name, name1, name2, axis_descr, desc, f):
    @Substitution(outname=name, desc=desc, name1=name1, name2=name2,
                  axis_descr=axis_descr)
    @Appender(_num_doc)
    def stat_func(self, axis=None, skipna=None, level=None, numeric_only=None,
                  **kwargs):
        _validate_kwargs(name, kwargs, 'out', 'dtype')

        if skipna is None:
            skipna = True
        if axis is None:
            axis = self._stat_axis_number
        if level is not None:
            return self._agg_by_level(name, axis=axis, level=level,
                                      skipna=skipna)
        return self._reduce(f, name, axis=axis, skipna=skipna,
                            numeric_only=numeric_only)

最后一行是关键。这有点有趣,因为 pandas.core 中大约有 7 个不同的 _reduces。 pdb 说它是 pandas.core.frame 中的那个。让我们来看看。

3)

def _reduce(self, op, name, axis=0, skipna=True, numeric_only=None,
            filter_type=None, **kwds):
    axis = self._get_axis_number(axis)

    def f(x):
        return op(x, axis=axis, skipna=skipna, **kwds)

    labels = self._get_agg_axis(axis)

    # exclude timedelta/datetime unless we are uniform types
    if axis == 1 and self._is_mixed_type and self._is_datelike_mixed_type:
        numeric_only = True

    if numeric_only is None:
        try:
            values = self.values
            result = f(values)
        except Exception as e:

            # try by-column first
            if filter_type is None and axis == 0:
                try:

                    # this can end up with a non-reduction
                    # but not always. if the types are mixed
                    # with datelike then need to make sure a series
                    result = self.apply(f, reduce=False)
                    if result.ndim == self.ndim:
                        result = result.iloc[0]
                    return result
                except:
                    pass

            if filter_type is None or filter_type == 'numeric':
                data = self._get_numeric_data()
            elif filter_type == 'bool':
                data = self._get_bool_data()
            else:  # pragma: no cover
                e = NotImplementedError("Handling exception with filter_"
                                        "type %s not implemented." %
                                        filter_type)
                raise_with_traceback(e)
            result = f(data.values)
            labels = data._get_agg_axis(axis)
    else:
        if numeric_only:
            if filter_type is None or filter_type == 'numeric':
                data = self._get_numeric_data()
            elif filter_type == 'bool':
                data = self._get_bool_data()
            else:  # pragma: no cover
                msg = ("Generating numeric_only data with filter_type %s"
                       "not supported." % filter_type)
                raise NotImplementedError(msg)
            values = data.values
            labels = data._get_agg_axis(axis)
        else:
            values = self.values
        result = f(values)

    if hasattr(result, 'dtype') and is_object_dtype(result.dtype):
        try:
            if filter_type is None or filter_type == 'numeric':
                result = result.astype(np.float64)
            elif filter_type == 'bool' and notnull(result).all():
                result = result.astype(np.bool_)
        except (ValueError, TypeError):

            # try to coerce to the original dtypes item by item if we can
            if axis == 0:
                result = com._coerce_to_dtypes(result, self.dtypes)

    return Series(result, index=labels)

天哪,谈谈失控功能。有人需要重构!让我们放大故障线:

if hasattr(result, 'dtype') and is_object_dtype(result.dtype):
    try:
        if filter_type is None or filter_type == 'numeric':
            result = result.astype(np.float64)

你最好相信最后一行被执行了。这是一些 pdb 跟踪:

> c:\users\matthew\anaconda2\lib\site-packages\pandas\core\frame.py(4801)_reduce()
-> result = result.astype(np.float64)
(Pdb) l
4796                result = f(values)
4797    
4798            if hasattr(result, 'dtype') and is_object_dtype(result.dtype):
4799                try:
4800                    if filter_type is None or filter_type == 'numeric':
4801 ->                     result = result.astype(np.float64)
4802                    elif filter_type == 'bool' and notnull(result).all():
4803                        result = result.astype(np.bool_)
4804                except (ValueError, TypeError):
4805    
4806                    # try to coerce to the original dtypes item by item if we can

如果您不相信,请打开 pandas.core.frame.py 并将 print "OI" 放在第 4801 行的正上方。它应该会弹出到控制台 :)。请注意,我使用的是 Anaconda 2,Windows。

我将使用“错误”来回答您的问题。

关于python - 为什么将字符串的总和转换为 float ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38470550/

相关文章:

python - Selenium - 在每一行代码之后等待

python - Pandas 将标量值添加到数字列?

python - 如何在不创建 conn.cursor() 的情况下检查/打印 psycopg2 动态查询 Compose

python - 返回特定日期两侧最近的日期 Python/Pandas

pandas - 在 Pandas 中连接连续行的简洁方法

python - 查找 DataFrame 中两列之间的时间差

python - 如何设置 PYTHONUTF8 环境变量以在 Python 中默认启用 UTF-8 编码?

Python 进度条显示额外的百分比

pandas - 让 Pandas DataFrame apply() 使用所有核心?

python - 在方法结束时传入无限元组作为参数