Skip to content

BUG: to_sql no longer raises an AttributeError when saving an OBB date #33140

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v1.1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,7 @@ I/O
- Bug in :meth:`read_csv` was causing a segfault when there were blank lines between the header and data rows (:issue:`28071`)
- Bug in :meth:`read_csv` was raising a misleading exception on a permissions issue (:issue:`23784`)
- Bug in :meth:`read_csv` was raising an ``IndexError`` when header=None and 2 extra data columns

- Bug in :meth:`DataFrame.to_sql` where an ``AttributeError`` was raised when saving an out of bounds date (:issue:`26761`)

Plotting
^^^^^^^^
Expand Down
3 changes: 2 additions & 1 deletion pandas/io/sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -978,7 +978,8 @@ def _sqlalchemy_type(self, col):
return TIMESTAMP(timezone=True)
except AttributeError:
# The column is actually a DatetimeIndex
if col.tz is not None:
# GH 26761 or an Index with date-like data e.g. 9999-01-01
if getattr(col, "tz", None) is not None:
return TIMESTAMP(timezone=True)
return DateTime
if col_type == "timedelta64":
Expand Down
8 changes: 8 additions & 0 deletions pandas/tests/io/test_sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -1480,6 +1480,14 @@ def test_datetime_with_timezone_roundtrip(self):
result["A"] = to_datetime(result["A"])
tm.assert_frame_equal(result, expected)

def test_out_of_bounds_datetime(self):
# GH 26761
data = pd.DataFrame({"date": datetime(9999, 1, 1)}, index=[0])
data.to_sql("test_datetime_obb", self.conn, index=False)
result = sql.read_sql_table("test_datetime_obb", self.conn)
expected = pd.DataFrame([pd.NaT], columns=["date"])
tm.assert_frame_equal(result, expected)

def test_naive_datetimeindex_roundtrip(self):
# GH 23510
# Ensure that a naive DatetimeIndex isn't converted to UTC
Expand Down