-1

I have a file in pickle format that I import to jupyter notebook using:

news_data = pd.read_pickle('/Users/Final/Data/news_articles cleaned.pickle')

Data has two columns as shown in this picture

The Date column is in the format: Weekday Date Month Year eg. Monday 3rd January 2011

How can I change this format to: Year-Month-Date eg. 2011-01-03 ?

  • What exactly is your question? Is it "how to change date format in `.pkl` file", or "how to convert dates when reading pickled dataframe", or "how to change date format of date column in dataframe"? Or some other variation? – ddejohn Feb 11 '22 at 06:09
  • At any rate, you can likely find a solution in [this](https://stackoverflow.com/questions/51822956/change-dd-mm-yyyy-date-format-of-dataframe-date-column-to-yyyy-mm-dd) thread, or any of the other dozens of threads like it. – ddejohn Feb 11 '22 at 06:11
  • @ddejohn I have another data set having the date format as yyyy-mm-dd. I want to merge both the data sets based on the date. But because of the difference in the date format I am unable to merge them. – Varinder Jot Feb 11 '22 at 06:22
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Feb 21 '22 at 19:52

1 Answers1

0

with this script, you can change the date format to what you mentioned:

How can I change this format to: Year-Month-Date eg. 2011-01-03 ?

import pandas as pd
news_data = pd.read_pickle('/Users/Final/Data/news_articles cleaned.pickle')

news_data.Date = pd.to_datetime(news_data.Date)
news_data.Date = news_data.Date.strftime("%Y-%m-%d")

The conversion happens where i mentioned news_data.Date.strftime("%Y-%m-%d").

Shayan
  • 5,165
  • 4
  • 16
  • 45