0

Hi I am trying to read data from MongoDB and trying to convert it into data frame but getting an error that says "("Could not convert ObjectId('620d3f43ae93743dbb6f6846') with type ObjectId: did not recognize Python value type when inferring an Arrow data type", 'Conversion failed for column _id with type object')".

my code looks like this:

import streamlit as st
import pymongo
import streamlit as st
from pymongo import MongoClient    
import pandas as pd
import json
import string
import io
import re
import time
import csv
from pandas import Timestamp
import certifi
import datetime
from pandas.io.json import json_normalize

client=pymongo.MongoClient()
connection = MongoClient("mongodb+srv://*****:****@cluster0.t4iwt.mongodb.net/testdb?retryWrites=true&w=majority",tlsCAFile=certifi.where())
db=connection["testdb"]    
collection=db["test"]
cursor = collection.find()
entries=list(cursor)
entries[:]
df=pd.DataFrame(entries)
st.write(df)
  • [pymongoarrow](https://mongo-arrow.readthedocs.io/) looks like an interesting package that may be useful to you. – rickhg12hs Mar 01 '22 at 19:59

1 Answers1

0

Change type of _id to str.

df = pd.DataFrame(entries)
df = df.astype({"_id": str})
st.write(df)
ferdy
  • 4,396
  • 2
  • 4
  • 16