df['Inicio'] = pd.to_datetime(df['Inicio']) Traceback (most recent call last): Cell In[24], line 1 df['Inicio'] = pd.to_datetime(df['Inicio']) NameError: name 'df' is not defined
The error message indicates that the variable `df` is not defined in the current scope. This means that you are trying to assign a value to `df['Inicio']` before `df` has been created. To fix this issue, you need to create a DataFrame object `df` before trying to assign a value to one of its columns. Here's an example: ```python import pandas as pd # Create a DataFrame object df = pd.DataFrame({ 'Inicio': ['2022-01-01', '2022-01-02', '2022-01-03'] }) # Convert the 'Inicio' column to datetime df['Inicio'] = pd.to_datetime(df['Inicio']) ``` Alternatively, if you are working with an existing DataFrame and you are trying to convert a column to datetime, make sure that the column exists in the DataFrame before trying to convert it. ```python import pandas as pd # Create a DataFrame object df = pd.DataFrame({ 'Inicio': ['2022-01-01', '2022-01-02', '2022-01-03'] }) # Check if the column exists if 'Inicio' in df.columns: # Convert the 'Inicio' column to datetime df['Inicio'] = pd.to_datetime(df['Inicio']) else: print("The column 'Inicio' does not exist in the DataFrame.") ``` If you are still getting the error, it might be because the column 'Inicio' does not exist in the DataFrame, or the column contains values that cannot be converted to datetime. In this case, you need to check the values in the column and make sure they are in the correct format.