Numberlong to datetime format conversion

Question:

I have a column in my dataframe, which I want to convert to an acceptable date format, so that I can plot it using plotly.

I tried the following methods to get it to work, but I believe the input string is supposed to be in the same format.
`

df['InsertedDate'] = pd.to_datetime(df['InsertedDate'], format='%Y%m%d')
df['InsertedDate'] = df['InsertedDate'].apply(lambda x: pd.to_datetime(str(x),format='%Y%m%d'))

My dataframe looks like this:

df = pd.DataFrame({'id': {0: 84, 1: 84, 2: 84, 3: 84, 4: 124},'commitDate': {0: '1617799976000',1: '1622430757000', 2: '1607286945000'}})

My original data is from MongoDB,which I exported in json, hence the numberLong format.

Any suggestions on how to perform this conversion?

Asked By: Brie MerryWeather

||

Answers:

I believe unit='ms' might be what you are looking for:

df['commitDate'] = pd.to_datetime(df['commitDate'], unit='ms')

df

will produce this:
enter image description here

Answered By: Daniel Schneider
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.