Converting Data Types

pandas
Published

February 21, 2024

Implicit Type Conversion (Automatic Type Conversion)

Python sometimes handles type conversion automatically, a process known as implicit type conversion. This typically happens when operators interact with different data types.

num_int = 10
num_float = 20.5
sum = num_int + num_float  # Integer is implicitly converted to float
print(sum)  # Output: 30.5
print(type(sum)) # Output: <class 'float'>

In this case, the integer num_int is implicitly converted to a float before the addition operation takes place, resulting in a float output. This automatic conversion simplifies coding but can sometimes lead to unexpected behavior if not fully understood.

Explicit Type Conversion (Type Casting)

For more control, you can explicitly convert data types using type casting functions. This is crucial when dealing with specific data formats or needing strict type control.

Converting to Integer (int())

The int() function converts a value to an integer. It truncates the decimal part for floating-point numbers and raises an error if the conversion is not possible.

float_num = 3.14159
int_num = int(float_num)  # Truncates the decimal part
print(int_num)  # Output: 3

string_num = "10"
int_num2 = int(string_num)
print(int_num2) # Output: 10

#Error Handling
try:
  invalid_int = int("abc")
except ValueError:
  print("Error: Cannot convert 'abc' to an integer")

Converting to Float (float())

The float() function converts a value to a floating-point number.

int_num = 10
float_num = float(int_num)
print(float_num)  # Output: 10.0

string_num = "3.14"
float_num2 = float(string_num)
print(float_num2) # Output: 3.14

Converting to String (str())

The str() function converts a value to its string representation. This is particularly useful when you need to display numbers or other data types within strings.

num = 10
string_num = str(num)
print("The number is: " + string_num)  # Output: The number is: 10

float_num = 3.14
string_float = str(float_num)
print(type(string_float)) # Output: <class 'str'>

Converting to Boolean (bool())

The bool() function converts a value to a boolean (True or False). Numbers convert to False if they are zero, otherwise True. Empty strings or lists convert to False, while non-empty ones convert to True.

num_zero = 0
bool_zero = bool(num_zero)  # False
print(bool_zero)

num_nonzero = 10
bool_nonzero = bool(num_nonzero)  # True
print(bool_nonzero)

empty_string = ""
bool_empty = bool(empty_string)  # False
print(bool_empty)

non_empty_string = "hello"
bool_non_empty = bool(non_empty_string) # True
print(bool_non_empty)

Handling Potential Errors

Remember that type conversion attempts can sometimes fail, particularly when converting strings to numbers. Always use try-except blocks to handle potential ValueError exceptions gracefully, preventing your program from crashing.