Skip to content

ENH: Add mapping of nullable columns to pandas.to_sql #63116

@benjnicholls

Description

@benjnicholls

Feature Type

  • Adding new functionality to pandas

  • Changing existing functionality in pandas

  • Removing existing functionality in pandas

Problem Description

I wish I could use pandas to programmatically create tables from CSVs using the to_sql method and add an additional column mapping to define if the column is nullable or not. My use case is based on consuming data dictionaries and generating tables based on those data dictionaries in a target database. It doesn't look like this feature is available yet.

Feature Description

Add a new parameter to to_sql, nullable.
def to_sql(self, ..., nullable: dict):
"""
Parameters
---------
...
nullable: dict, default {}
Specifies if a column can be null or not
and will raise errors if no values are in the target column

"""

if nullable:
	include_null_not_null_in_sql(nullable)

Alternative Solutions

Could also create an inference parameter

def to_sql(self, ..., infer_null: bool):
"""
Parameters
---------
...
infer_null: bool, default False
Infers nullability based on current values of DataFrame

"""

if infer_null:
            for col in self.cols:
                    if col.notna().all()
	                assign_not_nullable(col)
                    else:
                            assign_nullable(col)

Additional Context

If anyone has any suggestions on how best to implement this I would be more than willing to contribute this feature.

Metadata

Metadata

Assignees

No one assigned

    Labels

    EnhancementNeeds TriageIssue that has not been reviewed by a pandas team member

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions