Skip to content

Instantly share code, notes, and snippets.

@jdthorpe
Last active July 15, 2017 16:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jdthorpe/9c2082fdf0ae0a5f09a6f304158e673e to your computer and use it in GitHub Desktop.
Save jdthorpe/9c2082fdf0ae0a5f09a6f304158e673e to your computer and use it in GitHub Desktop.
DataFrameField Quirks

DataFrameField Quirks

The greatest quirk in the DataFrameField class comes from the fact that the Pandas DataFrame instances are coerced to a python dictionary via df.to_dict("list") as soon as it is bound to a MongoEngine.Document instance, and likewise a new Pandas DataFram instance is created from the stored dict when it is retrieved via dot notation. As result, when it is retrieved from document class via dot notation, a different instance is returned.

For example:

import pandas as pd
from mongoengine import Document
class my_doc(Document)
    df = DataFrameField()

# CREATE THE INVENTORY REQUEST DataFrame
df1 = pd.DataFrame({
    'goods': ['a', 'a', 'b', 'b', 'b'],
    'stock': [5, 10, 30, 40, 10],
    'category': ['c1', 'c2', 'c1', 'c2', 'c1'],
    'date': pd.to_datetime(['2014-01-01', '2014-02-01', '2014-01-06', '2014-02-09', '2014-03-09'])
})

doc_inst = my_doc()

doc_inst.df = df1
df2 = doc_inst.df

print(df1 is df2)
#> False -- df1 and df2 are two separate Pandas DataFrame instances
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment