0Pricing
Pandas & NumPy Academy · درس

التحويل باستخدام astype()

حوّل الأعمدة إلى int وfloat وstring وboolean وdatetime باستخدام astype()، وتعامل مع أخطاء التحويل الشائعة.

التحويل باستخدام astype() درس مجاني في Pandas & NumPy Academy على CoddyKit. هذا هو الدرس 2 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Pandas & NumPy Academy، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Pandas & NumPy Academy 4 دروس في المجموع.

بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.

Introduction to astype()

Series.astype(dtype) converts a column from its current type to the type you specify. It returns a new Series (or DataFrame when called on a full DataFrame) without modifying the original. This is the primary Pandas tool for fixing dtype problems after loading data, and it can convert between numeric types, to strings, to booleans, and to the Categorical type.

import pandas as pd

df = pd.DataFrame({'age': ['25', '30', '35']})
print('Before:', df['age'].dtype)  # object

df['age'] = df['age'].astype(int)
print('After:', df['age'].dtype)   # int64
print(df['age'] + 1)               # [26, 31, 36] — arithmetic now works

Converting to Numeric Types

The most common conversion is from object to a numeric type. You can cast to 'int64', 'int32', 'float64', 'float32', etc. If any value in the column cannot be converted, astype() raises a ValueError. Use pd.to_numeric(series, errors='coerce') instead when the column may contain non-numeric strings — it converts bad values to NaN rather than crashing.

import pandas as pd

df = pd.DataFrame({'price': ['10.5', '20.0', 'N/A', '30.5']})

# astype would crash on 'N/A'
# price_float = df['price'].astype(float)  # ValueError!

# pd.to_numeric with errors='coerce' is safer
df['price_float'] = pd.to_numeric(df['price'], errors='coerce')
print(df)
#   price  price_float
# 0  10.5         10.5
# 1  20.0         20.0
# 2   N/A          NaN
# 3  30.5         30.5

Converting to String (object)

Casting a column to str (or 'object') converts every value to its string representation. This is useful when you need to concatenate a numeric column with a text column, or when you want to apply string methods to numeric data like zero-padding IDs. Pandas also has the newer 'string' dtype that offers better NA-handling for text columns.

import pandas as pd

df = pd.DataFrame({'id': [1, 2, 3], 'region': ['E', 'W', 'N']})

# Concatenate id and region into a composite key
df['key'] = df['id'].astype(str) + '_' + df['region']
print(df)
#    id region  key
# 0   1      E  1_E
# 1   2      W  2_W
# 2   3      N  3_N

Converting to Boolean

Boolean conversion is useful when columns store True/False as integers (0/1) or as strings ('Yes'/'No'). Casting 0/1 integers with astype(bool) works directly: 0 becomes False, anything non-zero becomes True. For string 'Yes'/'No' columns, map first with a dictionary, then cast.

import pandas as pd

df = pd.DataFrame({
    'active_int': [1, 0, 1, 0],
    'active_str': ['Yes', 'No', 'Yes', 'No']
})

# Integer to bool
df['active_bool'] = df['active_int'].astype(bool)

# String to bool via mapping
df['active_bool2'] = df['active_str'].map({'Yes': True, 'No': False})

print(df[['active_bool', 'active_bool2']])
#    active_bool  active_bool2
# 0         True          True
# 1        False         False

Downcasting Numeric Types

By default, Pandas uses 64-bit types. If your integer values fit in 32 or even 8 bits, you can downcast to a smaller type to halve (or quarter) memory usage. Use pd.to_numeric(series, downcast='integer') or explicitly cast with astype('int32'). This is a key optimisation for large datasets.

import pandas as pd
import numpy as np

df = pd.DataFrame({'count': np.random.randint(0, 200, 1_000_000)})

print('int64 bytes:', df['count'].nbytes)   # 8,000,000

df['count_int16'] = df['count'].astype('int16')  # max 32767, fits 0-200
print('int16 bytes:', df['count_int16'].nbytes)  # 2,000,000

# Or let Pandas choose the smallest type automatically
df['count_auto'] = pd.to_numeric(df['count'], downcast='integer')
print('auto dtype:', df['count_auto'].dtype)

Converting Full DataFrames with astype(dict)

You can convert multiple columns at once by passing a dictionary to df.astype() where keys are column names and values are target dtypes. This is cleaner than chaining individual column assignments and makes the type-conversion step self-documenting as a single block in your pipeline.

import pandas as pd

df = pd.DataFrame({
    'age': ['25', '30'],
    'salary': ['50000', '70000'],
    'is_manager': ['1', '0']
})

df = df.astype({
    'age': 'int32',
    'salary': 'float64',
    'is_manager': 'bool'
})
print(df.dtypes)
# age           int32
# salary      float64
# is_manager     bool

Handling Errors in astype()

astype() has no built-in error-tolerant mode (unlike pd.to_numeric). If a conversion fails, it raises a ValueError or OverflowError. The safe workflow is: (1) clean the column first (strip symbols, fill NaN), (2) then cast. Alternatively, use pd.to_numeric(errors='coerce') for numerics or write a small helper function that catches conversion errors.

import pandas as pd

# Clean then cast pattern
df = pd.DataFrame({'price': ['$1,200', '$800', '$450']})

# Step 1: remove non-numeric characters
df['price_clean'] = (
    df['price']
    .str.replace('$', '', regex=False)
    .str.replace(',', '', regex=False)
)
# Step 2: safe cast
df['price_num'] = df['price_clean'].astype(float)
print(df)
#      price price_clean  price_num
# 0  $1,200        1200     1200.0
# 1    $800         800      800.0
# 2    $450         450      450.0

Casting to Pandas StringDtype

The newer 'string' dtype (capital S variant or pd.StringDtype()) was introduced to provide first-class string support with proper NA handling — it stores missing strings as pd.NA rather than None or np.nan. It enables the .str accessor while preserving NA semantics better than the object dtype, and is recommended for new code targeting Pandas 2+.

import pandas as pd

df = pd.DataFrame({'name': ['Alice', None, 'Carol']})

# Convert to the modern string dtype
df['name'] = df['name'].astype('string')
print(df['name'].dtype)   # string
print(df['name'].isna())  # proper NA detection
# 0    False
# 1     True
# 2    False

Overflow Risks When Downcasting

When you downcast to a smaller integer type, values that exceed the type's range silently overflow and wrap around. For example, casting 300 to int8 (range -128 to 127) produces 44 without raising an error. Always verify that your actual data range fits within the target type before downcasting to avoid subtle corruption.

import pandas as pd
import numpy as np

df = pd.DataFrame({'value': [100, 200, 300, 127, 128]})

# int8 range: -128 to 127
df['value_i8'] = df['value'].astype('int8')
print(df)
#    value  value_i8
# 0    100       100
# 1    200       -56   <- overflow! 200-256 = -56
# 2    300        44   <- overflow! 300-256 = 44
# 3    127       127
# 4    128      -128   <- overflow!

# Always check range first
print(df['value'].max())  # 300 > 127 — int8 is UNSAFE here

Verifying Conversions with a Dtype Map

After performing multiple type conversions, validate the final schema by comparing the actual dtypes to an expected map. This assertion pattern catches mistakes like a column that failed to convert (e.g., because NaN prevented int conversion). Run these checks at the end of your data loading function as a lightweight schema test.

import pandas as pd

df = pd.DataFrame({
    'user_id': [1, 2, 3],
    'amount': [10.5, 20.0, 30.5],
    'active': [True, False, True]
})

expected = {'user_id': 'int64', 'amount': 'float64', 'active': 'bool'}

for col, dtype in expected.items():
    assert str(df[col].dtype) == dtype, (
        f'{col}: expected {dtype}, got {df[col].dtype}'
    )
print('Schema validation passed')

Full Conversion Pipeline Example

Bringing it all together: here is a realistic post-read_csv conversion pipeline that cleans formatting, handles NaN safely, casts to optimal types, and validates the result. This pattern — clean, cast, validate — should be a standard part of every data ingestion function.

import pandas as pd

raw = pd.DataFrame({
    'user_id': ['101', '102', '103'],
    'revenue': ['$1,200', '$800', '$2,500'],
    'active': ['Yes', 'No', 'Yes']
})

df = (
    raw
    .assign(
        user_id=raw['user_id'].astype('int32'),
        revenue=pd.to_numeric(
            raw['revenue'].str.replace('[$,]', '', regex=True)
        ),
        active=raw['active'].map({'Yes': True, 'No': False})
    )
)
print(df.dtypes)
# user_id      int32
# revenue    float64
# active        bool

Quick Check

Test your understanding of casting with astype().

Lesson Recap

In this lesson you learned: astype(dtype) converts a column or entire DataFrame to a new type, pd.to_numeric(errors='coerce') is safer for columns with non-numeric strings, and passing a dictionary to astype() converts multiple columns at once. Downcast carefully to avoid overflow, and always validate the final schema with assertions. Next up we explore the memory-efficient Categorical dtype.

الأسئلة الشائعة

هل درس «التحويل باستخدام astype()» مجاني؟

نعم — نص درس «التحويل باستخدام astype()» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Pandas & NumPy Academy، انتقل إلى CoddyKit PRO. تتضمن دورة Pandas & NumPy Academy 4 دروس في المجموع.

ماذا ستتعلم في «التحويل باستخدام astype()»؟

حوّل الأعمدة إلى int وfloat وstring وboolean وdatetime باستخدام astype()، وتعامل مع أخطاء التحويل الشائعة. تتمرن على Pandas & NumPy Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.

هل أحتاج إلى خبرة سابقة لأبدأ Pandas & NumPy Academy؟

لا تُشترط خبرة سابقة. Pandas & NumPy Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 2 من أصل 4.

كم من الوقت يستغرق درس «التحويل باستخدام astype()»؟

معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.

هل يمكنني كتابة وتشغيل أكواد في درس Pandas & NumPy Academy هذا؟

نعم. كل درس في Pandas & NumPy Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.

جميع الدروس في هذه الدورة

  1. فحص أنواع بيانات الأعمدة
  2. التحويل باستخدام astype()
  3. نوع البيانات الفئوي
  4. تحليل التواريخ بطريقة صحيحة
← العودة إلى Pandas & NumPy Academy