|
1 | | -.. docs-landing/source/languages/python.txt |
2 | | - |
3 | | -Can {+driver-short+} Load the Results of a Query as a Pandas DataFrame? |
4 | | ------------------------------------------------------------------------ |
5 | | - |
6 | | -You can use the `PyMongoArrow <https://www.mongodb.com/docs/languages/python/pymongo-arrow-driver/current/>`__ |
7 | | -library to work with numerical or columnar data. PyMongoArrow lets you |
8 | | -load MongoDB query result-sets as |
9 | | -`Pandas DataFrames <https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.html>`__, |
10 | | -`NumPy ndarrays <https://numpy.org/doc/stable/reference/generated/numpy.ndarray.html>`__, or |
11 | | -`Apache Arrow Tables <https://arrow.apache.org/docs/python/generated/pyarrow.Table.html>`__. |
12 | | - |
13 | | -How Can I Encode My Documents to JSON? |
14 | | --------------------------------------- |
15 | | - |
16 | | -{+driver-short+} supports some special types, like ``ObjectId`` |
17 | | -and ``DBRef``, that aren't supported in JSON. Therefore, Python's ``json`` module won't |
18 | | -work with all documents in {+driver-short+}. Instead, {+driver-short+} includes the |
19 | | -`json_util <https://pymongo.readthedocs.io/en/latest/api/bson/json_util.html>`__ |
20 | | -module, a tool for using Python's ``json`` module with BSON documents and |
21 | | -`MongoDB Extended JSON <https://mongodb.com/docs/manual/reference/mongodb-extended-json/>`__. |
22 | | - |
23 | | -`python-bsonjs <https://pypi.python.org/pypi/python-bsonjs>`__ is another |
24 | | -BSON-to-MongoDB-Extended-JSON converter, built on top of |
25 | | -`libbson <https://github.com/mongodb/libbson>`__. python-bsonjs doesn't |
26 | | -depend on {+driver-short+} and might offer a performance improvement over |
27 | | -``json_util`` in certain cases. |
28 | | - |
29 | | -.. tip:: |
30 | | - |
31 | | - python-bsonjs works best with {+driver-short+} when using the ``RawBSONDocument`` |
32 | | - type. |
33 | | - |
34 | | -Does {+driver-short+} Behave Differently in Python 3? |
35 | | ------------------------------------------------------ |
36 | | - |
37 | | -{+driver-short+} encodes instances of the ``bytes`` class |
38 | | -as BSON type 5 (binary data) with subtype 0. |
39 | | -In Python 2, these instances are decoded to ``Binary`` |
40 | | -with subtype 0. In Python 3, they are decoded back to ``bytes``. |
41 | | - |
42 | | -The following code examples use {+driver-short+} to insert a ``bytes`` instance |
43 | | -into MongoDB, and then find the instance. |
44 | | -In Python 2, the byte string is decoded to ``Binary``. |
45 | | -In Python 3, the byte string is decoded back to ``bytes``. |
46 | | - |
47 | | -.. tabs:: |
48 | | - |
49 | | - .. tab:: Python 2.7 |
50 | | - :tabid: python-2 |
51 | | - |
52 | | - .. code-block:: python |
53 | | - |
54 | | - >>> import pymongo |
55 | | - >>> c = pymongo.MongoClient() |
56 | | - >>> c.test.bintest.insert_one({'binary': b'this is a byte string'}).inserted_id |
57 | | - ObjectId('4f9086b1fba5222021000000') |
58 | | - >>> c.test.bintest.find_one() |
59 | | - {u'binary': Binary('this is a byte string', 0), u'_id': ObjectId('4f9086b1fba5222021000000')} |
60 | | - |
61 | | - .. tab:: Python 3.7 |
62 | | - :tabid: python-3 |
63 | | - |
64 | | - .. code-block:: python |
65 | | - |
66 | | - >>> import pymongo |
67 | | - >>> c = pymongo.MongoClient() |
68 | | - >>> c.test.bintest.insert_one({'binary': b'this is a byte string'}).inserted_id |
69 | | - ObjectId('4f9086b1fba5222021000000') |
70 | | - >>> c.test.bintest.find_one() |
71 | | - {'binary': b'this is a byte string', '_id': ObjectId('4f9086b1fba5222021000000')} |
72 | | - |
73 | | -Similarly, Python 2 and 3 behave differently when {+driver-short+} parses JSON binary |
74 | | -values with subtype 0. In Python 2, these values are decoded to instances of ``Binary`` |
75 | | -with subtype 0. In Python 3, they're decoded into instances of ``bytes``. |
76 | | - |
77 | | -The following code examples use the ``json_util`` module to decode a JSON binary value |
78 | | -with subtype 0. In Python 2, the byte string is decoded to ``Binary``. |
79 | | -In Python 3, the byte string is decoded back to ``bytes``. |
80 | | - |
81 | | -.. tabs:: |
82 | | - |
83 | | - .. tab:: Python 2.7 |
84 | | - :tabid: python-2 |
85 | | - |
86 | | - .. code-block:: python |
87 | | - |
88 | | - >>> from bson.json_util import loads |
89 | | - >>> loads('{"b": {"$binary": "dGhpcyBpcyBhIGJ5dGUgc3RyaW5n", "$type": "00"}}') |
90 | | - {u'b': Binary('this is a byte string', 0)} |
91 | | - |
92 | | - .. tab:: Python 3.7 |
93 | | - :tabid: python-3 |
94 | | - |
95 | | - .. code-block:: python |
96 | | - |
97 | | - >>> from bson.json_util import loads |
98 | | - >>> loads('{"b": {"$binary": "dGhpcyBpcyBhIGJ5dGUgc3RyaW5n", "$type": "00"}}') |
99 | | - {'b': b'this is a byte string'} |
100 | | - |
101 | | -Can I Share Pickled ObjectIds Between Python 2 and Python 3? |
102 | | ------------------------------------------------------------- |
103 | | - |
104 | | -If you use Python 2 to pickle an instance of ``ObjectId``, |
105 | | -you can always unpickle it with Python 3. To do so, you must pass |
106 | | -the ``encoding='latin-1'`` option to the ``pickle.loads()`` method. |
107 | | -The following code example shows how to pickle an ``ObjectId`` in Python 2.7, and then |
108 | | -unpickle it in Python 3.7: |
109 | | - |
110 | | -.. code-block:: python |
111 | | - :emphasize-lines: 12 |
112 | | - |
113 | | - # Python 2.7 |
114 | | - >>> import pickle |
115 | | - >>> from bson.objectid import ObjectId |
116 | | - >>> oid = ObjectId() |
117 | | - >>> oid |
118 | | - ObjectId('4f919ba2fba5225b84000000') |
119 | | - >>> pickle.dumps(oid) |
120 | | - 'ccopy_reg\n_reconstructor\np0\n(cbson.objectid\...' |
121 | | - |
122 | | - # Python 3.7 |
123 | | - >>> import pickle |
124 | | - >>> pickle.loads(b'ccopy_reg\n_reconstructor\np0\n(cbson.objectid\...', encoding='latin-1') |
125 | | - ObjectId('4f919ba2fba5225b84000000') |
126 | | - |
127 | | -If you pickled an ``ObjectID`` in Python 2, and want to unpickle it in Python 3, |
128 | | -you must pass the ``protocol`` argument with a value of ``2`` or less to the |
129 | | -``pickle.dumps()`` method. |
130 | | -The following code example shows how to pickle an ``ObjectId`` in Python 3.7, and then |
131 | | -unpickle it in Python 2.7: |
132 | | - |
133 | | -.. code-block:: python |
134 | | - :emphasize-lines: 7 |
135 | | - |
136 | | - # Python 3.7 |
137 | | - >>> import pickle |
138 | | - >>> from bson.objectid import ObjectId |
139 | | - >>> oid = ObjectId() |
140 | | - >>> oid |
141 | | - ObjectId('4f96f20c430ee6bd06000000') |
142 | | - >>> pickle.dumps(oid, protocol=2) |
143 | | - b'\x80\x02cbson.objectid\nObjectId\nq\x00)\x81q\x01c_codecs\nencode\...' |
144 | | - |
145 | | - # Python 2.7 |
146 | | - >>> import pickle |
147 | | - >>> pickle.loads('\x80\x02cbson.objectid\nObjectId\nq\x00)\x81q\x01c_codecs\nencode\...') |
148 | | - ObjectId('4f96f20c430ee6bd06000000') |
0 commit comments