0

Consider a pd.Series which contains a date column with integers, e.g. 01052012. What is the best way to convert this?

Example:

        tDate
0    20040915
1    20041020
2    20041117
3    20041222
4    20050119
..        ...
203  20210818
204  20210915
205  20211020
206  20211117
207  20211222

Using pd.TimeStamp(x) outputs:

>> pd.Timestamp(x.data.tDate[0])
Timestamp('1970-01-01 00:00:00.020040915')

I can do loop over each element and do the following, but it's most likely not a good practice:

y = x.values[0][0])[:4]
m = x.values[0][0])[4:6]
d = x.values[0][0])[6:]
pd.Timestamp(int(y),int(m),int(d))

2
  • to be clear, 20040915 should result in 2004-09-15? Commented Oct 14, 2020 at 18:58
  • pd.datetime(x.data.tDate, format='%Y%m%d', errors='coerce'), considering tDate is a pandas series column Commented Oct 14, 2020 at 19:01

2 Answers 2

3

this should work as well

 df['tDate'] = df['tDate'].apply(lambda x: pd.Timestamp(str(x)))

Another option will be to just cast the whole column like this

 df['tDate'] = df['tDate'].astype(str).astype('datetime64')
Sign up to request clarification or add additional context in comments.

3 Comments

no need for the apply/lambda... pd.to_datetime(s.astype(str)) allows for adjustments if needed and is slightly more readable.
This is slightly faster than the answer by wpercy
You might need .astype("datetime64[ms]") or .astype("datetime64[ns]") in newer versions
0

I would first cast the integer as a string and use the strptime method of the datetime module.

Something like this:

In [3]: s = pd.Series([20040915,20041020])

In [4]: s = s.map(str).map(lambda x: datetime.datetime.strptime(x, '%Y%m%d'))

In [5]: s
Out[5]:
0   2004-09-15
1   2004-10-20
dtype: datetime64[ns]

As mentioned in the comments,

In [6]: s = pd.Series([20040915,20041020])

In [7]: pd.to_datetime(s.astype(str))
Out[7]:
0   2004-09-15
1   2004-10-20
dtype: datetime64[ns]

will give the same result and is likely faster with large series.

2 Comments

no need fo map, lambda or datetime here, simply pd.to_datetime(s.astype(str))
I took this as an accepted comment since it gives you more control

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.