Q&A 18 How do you inspect variable types in a dataset?

18.1 Explanation

Inspecting variable types is one of the first steps in understanding the structure of your dataset. It helps you:

  • Know whether variables are categorical, numeric, text, or dates
  • Identify potential conversion needs (e.g., strings to datetime)
  • Choose appropriate analysis techniques and visualization methods

Most tools provide a built-in way to inspect data types for each column.

18.2 Python Code

import pandas as pd

# Load a sample dataset
df = pd.DataFrame({
    "name": ["Alice", "Bob"],
    "age": [30, 25],
    "member": [True, False],
    "joined": pd.to_datetime(["2022-01-01", "2021-07-15"])
})

# Inspect data types of each column
print(df.dtypes)

# Optional: check class of an individual column
print(type(df["age"]))
name              object
age                int64
member              bool
joined    datetime64[ns]
dtype: object
<class 'pandas.core.series.Series'>

18.3 R Code

# Create a sample data frame
df <- data.frame(
  name = c("Alice", "Bob"),
  age = c(30, 25),
  member = c(TRUE, FALSE),
  joined = as.Date(c("2022-01-01", "2021-07-15"))
)

# Inspect structure of the dataset
str(df)
'data.frame':   2 obs. of  4 variables:
 $ name  : chr  "Alice" "Bob"
 $ age   : num  30 25
 $ member: logi  TRUE FALSE
 $ joined: Date, format: "2022-01-01" "2021-07-15"
# Optional: check class of individual columns
class(df$age)
[1] "numeric"