published fresh until

Can only use .str accessor with string values: pandas

Summary As of October 2026, inspect the failing Series before converting it to text or datetime, and retain the conversion result. A column that looks like dates may still contain Python objects. Do first: print(s.dtype) and count non-null value types (checked 2026-10-28).

Not for parsed dates with swapped day and month, see pandas-mixed-date-formats-day-month-swapped-use-explicit-format

For your agent
claude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp

Read-only, no account. Sign-in is only for trial reports: /connect.md

What this thread does

AttributeError: Can only use .str accessor with string values, not {inferred_dtype} or AttributeError: Can only use .dt accessor with datetimelike values — inspect the Series dtype before choosing a conversion.

A pandas accessor rejects the column's current representation even though its values may look like text or dates.

First action — run print(s.dtype); print(s.dropna().map(type).value_counts()) on the failing Series without printing private values.

Official source establishes accessor validation; {inferred_dtype} is a template placeholder replaced by the inferred type, and no reader data conversion has been tested.

Not for: successfully parsed dates with the wrong day/month interpretation; see the date-format thread.

What changed

  • Current string accessor source, checked 2026-10-28, includes the inferred type in the message. Older examples with an exclamation mark are not the current main-branch template; this is not evidence of a new regression.
  • Current datetime accessor source, checked 2026-10-28, selects an accessor based on supported temporal dtypes, rather than how values print. The text guide, pandas 3.1.1 documentation as of November 2026 (checked 2026-11-15), documents explicit string dtype conversion.

How to fix

1. Check the exact Series at the failure, not just the column as originally loaded. Count Python value types and missing values; inspect representative values privately. Object dtype alone does not prove that a column is text. 2. For .str, decide whether numeric-looking values are actually identifiers or measurements. If the field is genuinely text, the documented conversion is s.astype("string"). Untested experiment: assign text = s.astype("string"), then apply the intended operation, for example text.str.strip(). Do not stringify a numeric measurement just to make a string accessor available. Leading zeros already lost during numeric import cannot be recovered by this cast. 3. For .dt, parse the column into a separate candidate and keep the result. Untested experiment for date-only text whose declared layout is year-month-day:

parsed = pd.to_datetime(s, format="%Y-%m-%d", errors="raise")
years = parsed.dt.year

pd assumes import pandas as pd. The parameters come from the official to_datetime reference, checked 2026-10-28; the variable names and workflow are illustrative. Choose the format from the input contract. Numeric epochs need the correct unit, not this text-format example. 4. Assign the accepted conversion back to the intended DataFrame column if later code uses that column. Calling pd.to_datetime(s) and discarding the returned object leaves the original unchanged. 5. If .dt stops working after .dt.date, retain the original timestamp Series for subsequent operations. The date accessor reference, checked 2026-10-28, returns Python date objects with object dtype. Extract year or month from the timestamp Series before producing dates for an external interface. 6. For offset-bearing timestamps, review the reference's utc=True behavior. It converts aware inputs to UTC but treats naive inputs as already UTC; it cannot infer their intended local timezone. If errors="coerce" is used to inspect failures, count and review newly missing entries before accepting them.

What breaks

Diagnostic: .str fails on a numeric Series

The string-accessor constructor checks inferred values, including categorical values. Mixed object columns may pass initial validation yet fail a particular method or return missing output for nonstrings. A successful accessor lookup is not proof of clean input.

Diagnostic: .dt fails after .dt.date

A display containing dates is not sufficient: Python date objects and formatted timestamp strings are not the same representation as a datetime Series. Timedeltas and periods have their own supported .dt properties; do not convert every temporal failure with the same timestamp recipe.

What to test

Check text results, null preservation and identifiers with leading zeros. For dates, verify dtype, null count, expected year/month and timezone. Include all-null, mixed-type and invalid input. Confirm the downstream code reads the converted Series. Treat new NaT values as rejected data requiring review, not a successful fix.

Pitfalls

Do not replace an accessor failure with a blanket cast of the whole DataFrame. astype(str) can change missing-value representation; choose string dtype deliberately. Do not hide invalid dates by coercing them and then dropping rows without accounting for them.

Community evidence

  • Reporter achapkowski, pandas issue 20579, checked 2026-10-28, asks about dtype-specific custom accessors. Maintainer TomAugspurger confirms the expectation that invalid types raise AttributeError. This supports validation semantics, not a universal conversion recipe.
  • Reporter dcsaba89, pandas issue 46582, checked 2026-10-28, reproduces .dt.year working on timestamps and failing after .dt.date. The current official date reference independently explains the representation change.

A prompt to give your agent

Your training data may predate the current pandas accessor wording. Read the linked official accessor source, text guide, to_datetime and dt.date pages. Do not guess from how a column prints. Record dtype, non-null value types and missing counts without exposing private values. Establish the intended text or temporal meaning, propose one conversion and keep the result assigned. Test actual values, null preservation and timezone meaning; label unverified repairs Untested experiment.

Limits

No pandas pipeline or reader dataset was run. Historical community reports establish particular failure patterns, not their frequency in current releases. Main-branch error wording is not a promise for every installed release. Custom extension dtypes, numeric date encodings and ambiguous day/month parsing require their own input contracts.

Started from a post by @dcsaba89, packaged by the editor. Original post

For agents

Copy a short instruction for an agent that has WhatWorks connected, or open this page as Markdown.

# Can only use .str accessor with string values: pandas
Summary: As of October 2026, inspect the failing Series before converting it to text or datetime, and retain the conversion result. A column that looks like dates may still contain Python objects. Do first: `print(s.dtype)` and count non-null value types (checked 2026-10-28).
Copy for agent
get the whatworks thread pandas-attributeerror-str-dt-accessor-dtype and help me try it

Follow this thread

Following tells you when the verdict changes, a new version ships or a new option is added. Your agent mentions it the next time you ask WhatWorks.

Sign in with GitHub to follow. Nothing is posted for you.

Sign in with GitHub

Or ask your agent
follow the whatworks thread pandas-attributeerror-str-dt-accessor-dtype
WhatWorks

Connect your agent MCP

Paste this into your agent

Connect me to WhatWorks: add https://whatworks.dev/mcp/login and sign me in so I have an account. Steps: https://whatworks.dev/connect.md.

Or run this in your terminal
  • Claude Codeclaude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp/login && claude mcp login whatworks
  • Codexcodex mcp add whatworks --url https://whatworks.dev/mcp/login && codex mcp login whatworks