简体   繁体   English

Firestore 写入文档 numpy 的无效类型错误

[英]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.int64int不同,因此不是 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.

相关问题 Android Studio - 中止 Firebase Firestore 文档在时间限制后写入 - Android Studio - Abort Firebase Firestore document write after time limit Firestore addDoc 错误:文档参考无效。 文档引用必须有偶数个段,但 - Firestore addDoc error:Invalid document reference. Document references must have an even number of segments, but 将包含整数类型字段的文档添加到 firestore - add a document which contains fields of type integers to firestore iOS 错误“JSON 写入 (FIRTimestamp) 中的类型无效” - iOS error 'Invalid type in JSON write (FIRTimestamp)' 关于 Firestore 中的事务,如果我多次写入同一个文档,这些操作会被扁平化为一次写入吗? - Regarding transactions in Firestore, if I write to the same document multiple times, will those operations get flattened to a single write? 由于每个文档每秒写入 1 次限制而导致写入失败时,Firestore 是否会出错或重试? - Does Firestore through error or retries when write failed due to the 1 write per sec per document limitation? 尝试使用 React + Redux 工具包查询在 Firestore 上创建文档时挂钩调用无效 - Invalid Hook Call when trying to create document on Firestore with React + Redux Toolkit Query 有条件地更新 Firestore 文档 - Conditionally updating a Firestore document “FIRESTORE 内部断言失败:文档引用无效。文档引用必须有偶数个段,但 NewGame 有 1 个” - "FIRESTORE INTERNAL ASSERTION FAILED: Invalid document reference. Document references must have an even number of segments, but NewGame has 1" 将集合添加到 Firestore 中的文档 - Adding a collection to a document in Firestore
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM