This rule raises an error when the dtype parameter is not provided when using pandas.read_csv or
pandas.read_table.
The pandas library provides an easy way to load data from documents hosted locally or remotely, for example with the pandas.read_csv
or pandas.read_table functions:
import pandas as pd
df = pd.read_csv("my_file.csv")
Pandas will infer the type of each columns of the CSV file and specify the datatype accordingly, making this code perfectly valid. However this snippet of code does not convey the proper intent of the user, and can raise questions such as:
df? df? These questions arise as there are no descriptions of what kind of data is loaded into the data frame, making the code less understandable and harder to maintain.
A straightforward way to fix these issues is by providing the schema of the data through the usage of the dtype parameter.
To fix this issue provide the dtype parameter to the read_csv or read_table function.
import pandas as pd
def foo():
return pd.read_csv("my_file.csv") # Noncompliant: it is unclear which type of data the data frame holds.
import pandas as pd
def foo():
return pd.read_csv(
"my_file.csv",
dtype={'name': 'str', 'age': 'int'}) # Compliant