[英]Firestore write to document invalid type errors for numpy
I have a dictionary with over 1000 fields that I'm trying to write to Firestore as a document.我有一本包含 1000 多个字段的字典,我正试图将其作为文档写入 Firestore。 However, I keep getting invalid type errors like below:但是,我不断收到无效类型错误,如下所示:
'Cannot convert to a Firestore Value', 3, 'Invalid type', <class 'numpy.int64'>
'Cannot convert to a Firestore Value', False, 'Invalid type', <class 'numpy.bool_'>
Most of my fields are of these numpy types.我的大部分字段都属于这些 numpy 类型。 I'm new to Firestore so I'm not sure if I missing something simple or If I have to convert all my fields?我是 Firestore 的新手,所以我不确定我是否遗漏了一些简单的东西,或者我是否必须转换我的所有字段? Does the Firestore python library not allow numpy types? Firestore python 库是否不允许 numpy 类型? I have multiple nested dictionaries within the main dictionary so it won't be easy to convert everything to different types.我在主字典中有多个嵌套字典,因此将所有内容转换为不同类型并不容易。
Here is the python code I'm using to write to Firestore:这是我用来写入 Firestore 的 python 代码:
firestore_db.collection("collection_name").document("doc_name").set(allVarsDict)
Here is a sample of the dictionary:这是字典的示例:
{
'price_date': '2020-08-22 00:00:00',
'time_ms': 1598054400000,
'price1min': 11526.32,
'price_open1min': 11529.99,
'volume1min': 12.75662516,
'price': 11526.32,
'price1min_dict': {
'price_date': '2020-08-22 00:00:00',
'ms': 1598054400000,
'50ma': 11525.190199999994,
'128ma': 11547.188046874997,
'200ma': 11569.5485,
'200ema': 11552.031164880687,
'heightPer': 0.0013139635732870457,
'pinbarPer': 0.24224422442245286,
'hammer': True,
'star': False,
'green': False,
'pinbar': True,
'height': True,
'rsi': 49.58870472682622,
'Mflow': -3.0986389827593954,
'cmf': -0.39267912421581946,
'obv': 12.09637204,
'stoch': 85.27999999999884,
},
Thanks for any help!谢谢你的帮助!
NumPy has a slightly different way to represent data. NumPy 表示数据的方式略有不同。 In other words, numpy.int64
is not the same as int
and is therefore not one of the acceptable data types in Firestore换句话说, numpy.int64
与int
不同,因此不是 Firestore 中可接受的数据类型之一
There are many ways to overcome this problem.有很多方法可以克服这个问题。 Depending on how you are obtaining the data, there could be a more efficient approach.根据您获取数据的方式,可能会有更有效的方法。 However, with the information provided, this is the only way I could suggest which seems to work for me, which involves using ndarray.item method:但是,根据提供的信息,这是我可以建议的唯一方法,这似乎对我有用,其中涉及使用ndarray.item方法:
def np_to_fs(og_dict):
for k, v in og_dcit.items():
if type(v).__module__ == 'numpy':
og_dict[k] = v.item()
This function would convert all instances of values of types originating from numpy and replace them with python-native variables.此 function 将转换源自 numpy 的类型值的所有实例,并将它们替换为 python-native 变量。
Keep in mind that since you have nested dictionaries, you will have to call np_to_fs
multiple times, once for each dictionary, and that the function will replace the values in-place.请记住,由于您有嵌套的字典,因此您必须多次调用np_to_fs
,每个字典一次,并且 function 将就地替换这些值。
Hope this helps:)希望这可以帮助:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.