将numpy类型转换为python [英] Convert numpy type to python

查看:169
本文介绍了将numpy类型转换为python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有从熊猫生成的以下形式的词典列表.我想将其转换为json格式.

I have a list of dicts in the following form that I generate from pandas. I want to convert it to a json format.

list_val = [{1.0: 685}, {2.0: 8}]
output = json.dumps(list_val)

但是,json.dumps会引发错误:TypeError:685不可序列化JSON

However, json.dumps throws an error: TypeError: 685 is not JSON serializable

我猜这是从numpy到python(?)的类型转换问题.

I am guessing it's a type conversion issue from numpy to python(?).

但是,当我使用np.int32(v)转换数组中每个字典的值v时,仍然会引发错误.

However, when I convert the values v of each dict in the array using np.int32(v) it still throws the error.

这是完整的代码

            new = df[df[label] == label_new] 
            ks_dict = json.loads(content)
            ks_list = ks_dict['variables']
            freq_counts = []

            for ks_var in ks_list:

                    freq_var = dict()
                    freq_var["name"] = ks_var["name"]
                    ks_series = new[ks_var["name"]]
                    temp_df = ks_series.value_counts().to_dict()
                    freq_var["new"] = [{u: np.int32(v)} for (u, v) in temp_df.iteritems()]            
                    freq_counts.append(freq_var)

           out = json.dumps(freq_counts)

推荐答案

您似乎是正确的:

>>> import numpy
>>> import json
>>> json.dumps(numpy.int32(685))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.7/json/__init__.py", line 243, in dumps
    return _default_encoder.encode(obj)
  File "/usr/lib/python2.7/json/encoder.py", line 207, in encode
    chunks = self.iterencode(o, _one_shot=True)
  File "/usr/lib/python2.7/json/encoder.py", line 270, in iterencode
    return _iterencode(o, 0)
  File "/usr/lib/python2.7/json/encoder.py", line 184, in default
    raise TypeError(repr(o) + " is not JSON serializable")
TypeError: 685 is not JSON serializable

这里不幸的是,numpy数字的__repr__不能给您任何有关它们是 type 的提示.当它们不是( gasp )时,它们会伪装成int.最终,看起来json告诉您int不可序列化,但实际上,它告诉您此特定的np.int32(或您实际拥有的任何类型)不可序列化. (没有真正的惊喜-没有np.int32 可序列化).这也是为什么您不可避免地在将其传递给json.dumps之前在 上打印的字典看起来也只有整数的原因.

The unfortunate thing here is that numpy numbers' __repr__ doesn't give you any hint about what type they are. They're running around masquerading as ints when they aren't (gasp). Ultimately, it looks like json is telling you that an int isn't serializable, but really, it's telling you that this particular np.int32 (or whatever type you actually have) isn't serializable. (No real surprise there -- No np.int32 is serializable). This is also why the dict that you inevitably printed before passing it to json.dumps looks like it just has integers in it as well.

这里最简单的解决方法可能是编写自己的序列化器 1 :

The easiest workaround here is probably to write your own serializer1:

class MyEncoder(json.JSONEncoder):
    def default(self, obj):
        if isinstance(obj, numpy.integer):
            return int(obj)
        elif isinstance(obj, numpy.floating):
            return float(obj)
        elif isinstance(obj, numpy.ndarray):
            return obj.tolist()
        else:
            return super(MyEncoder, self).default(obj)

您可以这样使用它:

json.dumps(numpy.float32(1.2), cls=MyEncoder)
json.dumps(numpy.arange(12), cls=MyEncoder)
json.dumps({'a': numpy.int32(42)}, cls=MyEncoder)

1 或者您可以编写默认函数并将其作为defaut关键字参数传递给json.dumps.在这种情况下,您将用raise TypeError替换最后一行,但是...嗯.该类更具可扩展性:-)

1Or you could just write the default function and pass that as the defaut keyword argument to json.dumps. In this scenario, you'd replace the last line with raise TypeError, but ... meh. The class is more extensible :-)

这篇关于将numpy类型转换为python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆