This has been answered in the comments where it was noted that the following works:

df.astype({'date': 'datetime64[ns]'})

In addition, you can set the dtype when reading in the data:

pd.read_csv('path/to/file.csv', parse_dates=['date'])
🌐
Pandas
pandas.pydata.org › docs › reference › api › pandas.DataFrame.astype.html
pandas.DataFrame.astype — pandas 3.0.3 documentation
This method allows the conversion of the data types of pandas objects, including DataFrames and Series, to the specified dtype.
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-pandas-dataframe-astype
Pandas DataFrame.astype()-Python - GeeksforGeeks
June 26, 2025 - Explanation: astype() method is used with a dictionary to convert 'A' to integer and 'B' to float. Both columns are now in numeric form, suitable for calculations. Example 3: In this, we try to convert column 'A' to integer, but due to a non-numeric ...
Discussions

python - Pandas 'astype' with date (or datetime) - Stack Overflow
This answer contains a very elegant way of setting all the types of your pandas columns in one line: # convert column "a" to int64 dtype and "b" to complex type df = df.astype({... More on stackoverflow.com
🌐 stackoverflow.com
python - Pandas: change data type of Series to String - Stack Overflow
The id Series consists of some integers and strings. Its dtype by default is object. I want to convert all contents of id to strings. I tried astype(str), which produces the output below. More on stackoverflow.com
🌐 stackoverflow.com
python - Fastest way to cast all dataframe columns to float - pandas astype slow - Stack Overflow
It is faster but it destroys the column names. When setting back the column names it takes similar time as doing df.astype(np.float64). +1 For my application, I didn't need all the column names back so I partially set the column names I needed, and this led to a huge performance boost! More on stackoverflow.com
🌐 stackoverflow.com
Pandas astype('str) does not change the column to string

The object dtype is how pandas stores strings in dataframes. If for some particular reason you absolutely can't have that data as an object dtype, you're going to have to save it to something other than a dataframe.

https://stackoverflow.com/questions/21018654/strings-in-a-dataframe-but-dtype-is-object

More on reddit.com
🌐 r/learnpython
2
3
May 11, 2018
People also ask

How can I prevent astype() from modifying the original DataFrame?
Set copy=True (default) to avoid modifying the original DataFrame. Use copy=False to modify in place.
🌐
upgrad.com
upgrad.com › home › blog › data science › a comprehensive guide to pandas dataframe astype()
A Comprehensive Guide to Pandas DataFrame astype()
What should I do if astype() fails with ValueError?
Use errors="coerce" to convert invalid values to NaN instead of breaking.
🌐
upgrad.com
upgrad.com › home › blog › data science › a comprehensive guide to pandas dataframe astype()
A Comprehensive Guide to Pandas DataFrame astype()
Can astype() handle conversions of string-based categorical columns?
Yes, convert string-based columns to category with astype('category') to optimize memory usage and speed up operations like groupby().
🌐
upgrad.com
upgrad.com › home › blog › data science › a comprehensive guide to pandas dataframe astype()
A Comprehensive Guide to Pandas DataFrame astype()
🌐
Medium
medium.com › @heyamit10 › understanding-pandas-astype-with-examples-f1b21ad17e69
Understanding pandas astype() with Examples | by Hey Amit | Medium
March 6, 2025 - This is where astype() truly shines because data isn’t always clean, tidy, or in the format you expect. You’ll often need to tweak it, and that’s exactly what we’ll do here. ... Imagine you’ve got a DataFrame with mixed data types — some numbers stored as integers, others as strings. Instead of converting each column one by one (which is tedious), you can handle them all at once using a dictionary. ... import pandas as pd # Sample DataFrame with mixed data types data = {'A': [1, 2, 3], 'B': ['4', '5', '6']} df = pd.DataFrame(data) # Convert column A to float and B to integer df = df.astype({'A': 'float', 'B': 'int'}) # Check the new data types print(df.dtypes)
Top answer
1 of 2
36

This has been answered in the comments where it was noted that the following works:

df.astype({'date': 'datetime64[ns]'})

In addition, you can set the dtype when reading in the data:

pd.read_csv('path/to/file.csv', parse_dates=['date'])
2 of 2
8

datetime

Since you can't pass datetime format to astype(), it's a little primitive and it's better to use pd.to_datetime() instead.

df['date'] = pd.to_datetime(df['date'])

For example, if the dates in the data are of the format %d/%m/%Y such as 01/04/2020, astype() would incorrectly parse it as 2020-01-04 whereas with pd.to_datetime(), you can pass the correct format.

If you need to convert multiple columns into datetime64 (which is often the reason astype() is used), then you can apply pd.to_datetime().

df = pd.DataFrame({'date1': ['01/04/2020'], 'date2': ['02/04/2020']})
df = df.apply(pd.to_datetime, format='%d/%m/%Y')

Even with read_csv, you have some control over the format, e.g.

df = pd.read_csv('file.csv', parse_dates=['date'], dayfirst=True)

date

If you want to cast into date, then you can first cast to datetime64[ns] and then use dt.date to get a column of datetime.date objects:

df['date'] = pd.to_datetime(df['date']).dt.date

The column dtype will become object though (on which you can still perform vectorized operations such as adding days, comparing dates etc.), so if you plan to work on it a lot in pandas, it's more performative to use datetime64 instead. For example, adding a day is extremely fast on datetime64 columns, not so much on date columns:

s_dt = pd.Series(pd.date_range('1700', None, 10000, 'D'))
s_d = s_dt.dt.date

%timeit x = s_dt + pd.Timedelta(days=1)
# 344 µs ± 17.3 µs per loop (mean ± std. dev. of 7 runs, 1,000 loops each)

%timeit y = s_d + pd.Timedelta(days=1)
# 56.1 ms ± 11.2 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)

With that being said, if you dump it into a database (such as sqlite), an object dtype column of datetime.date objects stored as a DATE type (whereas datetime64[ns] will be stored as TIMESTAMP).


Pandas datetime dtype is from numpy datetime64, so if you have pandas<2.0, you can use the following as well (since pandas 2.0, unitless datetime64 is not supported anymore). There's no date dtype (although you can perform vectorized operations on a column that holds datetime.date values).

df = df.astype({'date': np.datetime64})

# or (on a little endian system)
df = df.astype({'date': '<M8'})
# (on a big endian system)
df = df.astype({'date': '>M8'})
🌐
Pandas
pandas.pydata.org › docs › dev › reference › api › pandas.Series.astype.html
pandas.Series.astype — pandas documentation
This method allows the conversion of the data types of pandas objects, including DataFrames and Series, to the specified dtype.
Find elsewhere
🌐
Pandas
pandas.pydata.org › docs › reference › api › pandas.Index.astype.html
pandas.Index.astype — pandas 3.0.3 documentation
Index.astype(dtype, copy=True)[source]# Create an Index with values cast to dtypes. The class of a new Index is determined by dtype. When conversion is impossible, a TypeError exception is raised. Parameters: dtypenumpy dtype or pandas type · Note that any signed integer dtype is treated as 'int64', and any unsigned integer dtype is treated as 'uint64', regardless of the size.
🌐
Snowflake Documentation
docs.snowflake.com › en › developer-guide › snowpark › reference › python › 1.30.0 › modin › pandas_api › modin.pandas.DataFrame.astype
modin.pandas.DataFrame.astype | Snowflake Documentation
DataFrame.astype(dtype: str | type | pd.Series | dict[str, type], copy: bool = True, errors: Literal['raise', 'ignore'] = 'raise') → pd.DataFrame | pd.Series[source]¶ · Cast a pandas object to a specified dtype dtype. Parameters: dtype (str, data type, Series or Mapping of column name -> data type) – Use a str, numpy.dtype, pandas.ExtensionDtype or Python type to cast entire pandas object to the same type.
🌐
Upgrad
upgrad.com › home › blog › data science › a comprehensive guide to pandas dataframe astype()
A Comprehensive Guide to Pandas DataFrame astype()
February 3, 2025 - Pandas astype() is used to cast a DataFrame column (or multiple columns) to a specified data type. It allows you to convert data types, such as changing integers to floats, strings to categorical types, or objects to datetime.
🌐
Medium
medium.com › @bacharliav › boosting-memory-efficiency-in-pandas-replacing-astype-str-with-convert-dtypes-1e7675ceca76
Boosting Memory Efficiency in Pandas: Replacing “astype(str)” with “convert_dtypes” | by Bacharliav | Medium
June 30, 2023 - In df_astype_str, each missing value stores the three characters 'n', 'a', 'n' instead of just a single NA indicator, thereby demanding additional memory space. The resolution to this memory challenge lies in a less commonly known, yet incredibly powerful function in pandas — pd.convert_dtypes().
🌐
Pandas
pandas.pydata.org › pandas-docs › version › 2.1 › reference › api › pandas.DataFrame.astype.html
pandas.DataFrame.astype — pandas 2.1.4 documentation
>>> ser = pd.Series([1, 2], dtype='int32') >>> ser 0 1 1 2 dtype: int32 >>> ser.astype('int64') 0 1 1 2 dtype: int64 ... >>> from pandas.api.types import CategoricalDtype >>> cat_dtype = CategoricalDtype( ...
Top answer
1 of 11
251

A new answer to reflect the most current practices: as of now (v1.2.4), neither astype('str') nor astype(str) work.

As per the documentation, a Series can be converted to the string datatype in the following ways:

df['id'] = df['id'].astype("string")

df['id'] = pandas.Series(df['id'], dtype="string")

df['id'] = pandas.Series(df['id'], dtype=pandas.StringDtype)

End to end example:

import pandas as pd

# Create a sample DataFrame
data = {
    'Name': ['John', 'Alice', 'Bob', 'John', 'Alice'],
    'Age': [25, 30, 35, 25, 30],
    'City': ['New York', 'London', 'Paris', 'New York', 'London'],
    'Salary': [50000, 60000, 70000, 50000, 60000],
    'Category': ['A', 'B', 'C', 'A', 'B']
}

df = pd.DataFrame(data)

# Print the DataFrame
print("Original DataFrame:")
print(df)
print("\nData types:")
print(df.dtypes)
cat_cols_ = None
# Apply the code to change data types
if not cat_cols_:
    # Get the columns with object data type
    object_columns = df.select_dtypes(include=['object']).columns.tolist()
    
    if len(object_columns) > 0:
        print(f"\nObject columns found, converting to string: {object_columns}")
        
        # Convert object columns to string type
        df[object_columns] = df[object_columns].astype('string')
    
    # Get the categorical columns (including string and category data types)
    cat_cols_ = df.select_dtypes(include=['category', 'string']).columns.tolist()

# Print the updated DataFrame and data types
print("\nUpdated DataFrame:")
print(df)
print("\nUpdated data types:")
print(df.dtypes)
print(f"\nCategorical columns (cat_cols_): {cat_cols_}")
Original DataFrame:
    Name  Age      City  Salary Category
0   John   25  New York   50000        A
1  Alice   30    London   60000        B
2    Bob   35     Paris   70000        C
3   John   25  New York   50000        A
4  Alice   30    London   60000        B

Data types:
Name        object
Age          int64
City        object
Salary       int64
Category    object
dtype: object

Object columns found, converting to string: ['Name', 'City', 'Category']

Updated DataFrame:
    Name  Age      City  Salary Category
0   John   25  New York   50000        A
1  Alice   30    London   60000        B
2    Bob   35     Paris   70000        C
3   John   25  New York   50000        A
4  Alice   30    London   60000        B

Updated data types:
Name        string[python]
Age                  int64
City        string[python]
Salary               int64
Category    string[python]
dtype: object

Categorical columns (cat_cols_): ['Name', 'City', 'Category']
2 of 11
127

You can convert all elements of id to str using apply

df.id.apply(str)

0        123
1        512
2      zhub1
3    12354.3
4        129
5        753
6        295
7        610

Edit by OP:

I think the issue was related to the Python version (2.7.), this worked:

df['id'].astype(basestring)
0        123
1        512
2      zhub1
3    12354.3
4        129
5        753
6        295
7        610
Name: id, dtype: object
🌐
Reddit
reddit.com › r/learnpython › pandas astype('str) does not change the column to string
r/learnpython on Reddit: Pandas astype('str) does not change the column to string
May 11, 2018 -

Hey,

I have tried a lot of options for changing a pandas dataframe column values from object type to string type. However, the datatype does not change. Does any one of you have any ideas?

Before

ltctest.info()

Gives: <class 'pandas.core.frame.DataFrame'> Int64Index: 100 entries, 144334 to 144434 Data columns (total 6 columns): author 100 non-null object body 100 non-null object created_utc 100 non-null int64 id 100 non-null object score 100 non-null int64 datetime 100 non-null datetime64[ns] dtypes: datetime64ns, int64(2), object(3) memory usage: 5.5+ KB

ltctest['body'] = ltctest['body'].astype(str)

Results in the same info()

<class 'pandas.core.frame.DataFrame'> Int64Index: 100 entries, 144334 to 144434 Data columns (total 6 columns): author 100 non-null object body 100 non-null object created_utc 100 non-null int64 id 100 non-null object score 100 non-null int64 datetime 100 non-null datetime64[ns] dtypes: datetime64ns, int64(2), object(3) memory usage: 5.5+ KB

Thanks!

🌐
Practical Business Python
pbpython.com › pandas_dtypes.html
Overview of Pandas Data Types - Practical Business Python
The simplest way to convert a pandas column of data to a different type is to use astype() .
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-pandas-series-astype-to-convert-data-type-of-series
Python | Pandas Series.astype() to convert Data type of series - GeeksforGeeks
July 11, 2025 - After that some columns are converted using .astype() method and the dtypes are viewed again to see the changes. ... # importing pandas module import pandas as pd # reading csv file from url data = pd.read_csv("https://media.geeksforgeeks.org/wp-content/uploads/nba.csv") # dropping null value columns to avoid errors data.dropna(inplace = True) # storing dtype before converting before = data.dtypes # converting dtypes using astype data["Salary"]= data["Salary"].astype(int) data["Number"]= data["Number"].astype(str) # storing dtype after converting after = data.dtypes # printing to compare print("BEFORE CONVERSION\n", before, "\n") print("AFTER CONVERSION\n", after, "\n") Output: As shown in the output image, the data types of columns were converted accordingly.
🌐
W3Schools
w3schools.com › python › pandas › ref_df_astype.asp
Pandas DataFrame astype() Method
The astype() method returns a new DataFrame where the data types has been changed to the specified type. You can cast the entire DataFrame to one specific data type, or you can use a Python Dictionary to specify a data type for each column, ...
🌐
Note.nkmk.me
note.nkmk.me › home › python › pandas
pandas: How to use astype() to cast dtype of DataFrame | note.nkmk.me
August 9, 2023 - You can cast the data type (dtype) with the method astype() of DataFrame and Series. pandas.DataFrame.astype — pandas 2.0.3 documentation
🌐
DEV Community
dev.to › atifwattoo › what-is-astype-function-in-python-4bg5
What is astype() function in Python - DEV Community
January 8, 2025 - The astype() function is used to cast the data type of a pandas object (like a Series or DataFrame) or a NumPy array into another type.