Skip to content

Commit de4e380

Browse files
committed
BUG: check is_named_tuple for objects that are not subclass of tuple pandas-dev#40682
1 parent cf791c8 commit de4e380

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

pandas/core/dtypes/inference.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ def is_named_tuple(obj) -> bool:
315315
>>> is_named_tuple((1, 2))
316316
False
317317
"""
318-
return isinstance(obj, tuple) and hasattr(obj, "_fields")
318+
return isinstance(obj, abc.Sequence) and hasattr(obj, "_fields")
319319

320320

321321
def is_hashable(obj) -> bool:

pandas/tests/io/test_sql.py

+37
Original file line numberDiff line numberDiff line change
@@ -2189,6 +2189,43 @@ def test_bigint_warning(self):
21892189
with tm.assert_produces_warning(None):
21902190
sql.read_sql_table("test_bigintwarning", self.conn)
21912191

2192+
def test_row_object_is_named_tuple(self):
2193+
# GH 40682
2194+
# Test for the is_named_tuple() function
2195+
# Placed here due to its usage of sqlalchemy
2196+
2197+
from sqlalchemy import (
2198+
Column,
2199+
Integer,
2200+
String,
2201+
)
2202+
from sqlalchemy.orm import (
2203+
declarative_base,
2204+
sessionmaker,
2205+
)
2206+
2207+
BaseModel = declarative_base()
2208+
2209+
class Test(BaseModel):
2210+
__tablename__ = "test_Frame"
2211+
id = Column(Integer, primary_key=True)
2212+
foo = Column(String(50))
2213+
2214+
BaseModel.metadata.create_all(self.conn)
2215+
Session = sessionmaker(bind=self.conn)
2216+
session = Session()
2217+
2218+
df = pd.DataFrame({"id": [0, 1], "foo": ["hello", "world"]})
2219+
df.to_sql("test_Frame", con=self.conn, index=False, if_exists="replace")
2220+
2221+
session.commit()
2222+
foo = session.query(Test.id, Test.foo)
2223+
print(foo)
2224+
df = pd.DataFrame(foo)
2225+
session.close()
2226+
2227+
assert list(df.columns) == ["id", "foo"]
2228+
21922229

21932230
class _TestMySQLAlchemy:
21942231
"""

0 commit comments

Comments
 (0)