'pd.to_datetime results in ValueError because of format [closed]

I downloaded apple stock data into a CSV file from yahoo finance and is trying to convert the date to datetime and it keeps giving errors. Below is my code:

import pandas as pd
import datetime
import matplotlib.pyplot as plt

df = pd.read_csv("AAPL.csv", usecols=["Date", "Close"], parse_dates=True)
df["Date2"] = pd.to_datetime(df["Date"], format='%y-%m-%d')
print(df.head())

I keep getting the following error: ValueError: time data '1980-12-12' does not match format '%y-%m-%d' (match)

Any help is appreciated.



Solution 1:[1]

import pandas as pd
import datetime
import matplotlib.pyplot as plt

df = pd.read_csv("AAPL.csv", usecols=["Date", "Close"], parse_dates=True)
df["Date2"] = pd.to_datetime(df["Date"], format='%Y-%m-%d')
print(df.head())

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Nsahu