0

I have a dataframe with tickers, returns, and dates in the format, for example, of "20200101". I'm trying to convert these values to Datetime values. However, when I attempt the following:

fin_data['DATE'] = pd.to_datetime(fin_data['DATE'])

The output is recognizing the date I have (which I'd like to be in YYYY-MM-DD format) as nanoseconds, so the output is something like this:

1970-01-01 00:00:00.020200101

Any help would be greatly appreciated.

2
  • Desired output is 2020-01-01, right? Commented Oct 24, 2021 at 2:58
  • Why is it an integer in the first place? If you're reading from a CSV for example, you can specify, pd.read_csv(parse_dates=['DATE']). Commented Oct 24, 2021 at 3:07

1 Answer 1

1

Specify the format:

fin_data = pd.DataFrame({'DATE': [20200101]})

pd.to_datetime(fin_data['DATE'], format='%Y%d%m')

Output:

0   2020-01-01
Name: DATE, dtype: datetime64[ns]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.