{"id":8106,"library":"django-pandas","title":"Django Pandas Integration","description":"django-pandas provides tools to seamlessly integrate Django models with Pandas DataFrames. It allows you to query your Django models and return the results directly as Pandas DataFrames, simplifying data analysis, reporting, and machine learning workflows within Django projects. As of version 0.6.7, it's actively maintained with releases focused on compatibility with recent Django and Pandas versions.","status":"active","version":"0.6.7","language":"en","source_language":"en","source_url":"https://github.com/chrisdev/django-pandas/","tags":["django","pandas","data-analysis","orm","dataframe","data-integration"],"install":[{"cmd":"pip install django-pandas","lang":"bash","label":"Install stable version"}],"dependencies":[{"reason":"Required for integration with Django projects (>=2.2)","package":"Django","optional":false},{"reason":"Core data structure and analysis library (>=1.0)","package":"pandas","optional":false}],"imports":[{"note":"DataFrameManager is located within the managers submodule.","wrong":"from django_pandas import DataFrameManager","symbol":"DataFrameManager","correct":"from django_pandas.managers import DataFrameManager"}],"quickstart":{"code":"from django.db import models\nfrom django_pandas.managers import DataFrameManager\n\n# Define a simple Django model\nclass Product(models.Model):\n    name = models.CharField(max_length=255)\n    price = models.DecimalField(max_digits=10, decimal_places=2)\n    stock = models.IntegerField(default=0)\n    last_updated = models.DateTimeField(auto_now=True)\n\n    # Attach DataFrameManager to your model\n    objects = DataFrameManager()\n\n    def __str__(self):\n        return self.name\n\n# --- Usage example (in a Django shell or view) ---\n# Make sure to run migrations for the Product model first.\n# from myapp.models import Product # Assuming Product is in 'myapp'\n# Product.objects.create(name='Laptop', price=1200.00, stock=50)\n# Product.objects.create(name='Mouse', price=25.50, stock=200)\n\n# Fetch data directly as a Pandas DataFrame\n# df = Product.objects.to_dataframe()\n# print(df.head())\n\n# You can also filter before converting to DataFrame\n# low_stock_df = Product.objects.filter(stock__lt=100).to_dataframe()\n# print(low_stock_df)\n","lang":"python","description":"To use django-pandas, add `objects = DataFrameManager()` to your Django model. Then, you can call `.to_dataframe()` on your model's manager or any QuerySet to convert the results into a Pandas DataFrame. Remember to ensure your Django app is configured and migrations are run."},"warnings":[{"fix":"Apply `.filter()` or `.values()` to select specific rows/columns before calling `.to_dataframe()`. For extremely large datasets, consider iterating over chunks or using database-level aggregations first.","message":"Converting very large QuerySets to DataFrames directly can consume significant memory and impact performance, especially for tables with millions of rows. Consider filtering or chunking your data.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Use the `related` argument in `to_dataframe()` (e.g., `to_dataframe(related=['foreign_key_field__name'])`) or use `.select_related()` on your QuerySet before calling `to_dataframe()` for one-to-one/many-to-one relationships, or `.prefetch_related()` for many-to-many/one-to-many.","message":"By default, `to_dataframe()` does not automatically include related fields (e.g., ForeignKeys) as their actual values; it typically includes their IDs. To include related object data, you need to specify them.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Always check the `install_requires` in the `pyproject.toml` or `setup.py` on the GitHub repository for the exact version ranges supported. As of 0.6.7, Django>=2.2 and pandas>=1.0 are required. Upgrade or downgrade your Django/Pandas installations as necessary.","message":"django-pandas has specific compatibility requirements for Django and Pandas versions. Using incompatible versions can lead to unexpected errors or silent failures.","severity":"breaking","affected_versions":"<0.6.0 (older Django/Pandas versions), >0.6.7 (future versions)"}],"env_vars":null,"last_verified":"2026-04-16T00:00:00.000Z","next_check":"2026-07-15T00:00:00.000Z","problems":[{"fix":"Ensure your model definition includes `objects = DataFrameManager()`:\n\n```python\nfrom django_pandas.managers import DataFrameManager\n\nclass MyModel(models.Model):\n    # ... fields ...\n    objects = DataFrameManager() # Add this line\n```","cause":"The `DataFrameManager` has not been correctly assigned to your Django model, or you are trying to call `to_dataframe()` on Django's default manager.","error":"AttributeError: 'Manager' object has no attribute 'to_dataframe'"},{"fix":"Install the package using pip: `pip install django-pandas`","cause":"The `django-pandas` library is not installed in your current Python environment.","error":"ModuleNotFoundError: No module named 'django_pandas'"},{"fix":"Always call a queryset method before iterating or converting to a DataFrame. For example, `MyModel.objects.all().to_dataframe()` or `df = MyModel.objects.to_dataframe()`.","cause":"You are trying to iterate directly over `MyModel.objects` without first calling a queryset method (like `.all()`) or `to_dataframe()`.","error":"TypeError: 'DataFrameManager' object is not iterable"}]}