BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
sastpw
SAS Employee

Good deal, glad that got you going. Yes, good job on the second one. I think even just doing mydata['Col2'] =  str(mydata['Col2']) would work too; str() of about anything gives you the string representation. But, there's a lot of different ways to accomplish something in python, so whatever works.

 

For the first column, I'm no pandas expert, so I had to play with this and figure it out. I found a case that worked. Again, could be lots of ways to do this kind of thing. Here's what I found:

 

>>> pd.to_datetime('20NOV2007:00:00:00', format='%d%b%Y:%H:%M:%S')
Timestamp('2007-11-20 00:00:00')

 

The doc for pandas.to_datetime is here: https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.to_datetime.html

and under the format= parm it points to the formatting codes to use: https://docs.python.org/3/library/datetime.html#strftime-and-strptime-format-codes

 

See if that solves this for you.

Tom

 

>>> df
a
0 20NOV2007:00:00:00
>>> df.ftypes
a object:dense
dtype: object

 

>>> df['a'] = pd.to_datetime(df['a'], format='%d%b%Y:%H:%M:%S')
>>> df
a
0 2007-11-20
>>> df.dtypes
a datetime64[ns]
dtype: object
>>>

AshishM
Fluorite | Level 6

Hi Tom,

Thanks a lot for all your help and guidance.  I went through your original post again and came up with the same approach. I referred to the same link to come to up with the same solution :). I am new to Pandas, SAS and python so learning them slowly and spending a lot of time to solve issues.

I am now stuck with another issue, will try my best to fix it if not then I'll post it again.

 

Thanks again for your time. Much appreciated.

Ashish

sastpw
SAS Employee

That's tough trying to learn all 3 at the same time. Good job for doing so!

Here are a few links that I always have open, beyond just googling things, that I've found to be very helpful as I've taught myself Python. You can look at whatever version you like:

https://docs.python.org/3.5/index.html  has 2 links that I use the most:

https://docs.python.org/3.5/library/index.html   

https://docs.python.org/3.5/reference/index.html

 

And for Pandas, specifically: https://pandas.pydata.org/pandas-docs/stable/index.html

The API reference is key: https://pandas.pydata.org/pandas-docs/stable/reference/index.html

 

And don't forget SAS, it has more tricks than the other two:

https://go.documentation.sas.com/?cdcId=pgmsascdc&cdcVersion=9.4_3.5&docsetId=pgmsashome&docsetTarge...

 

Hopefully these references will help you along your journey; they have for me.

 

Tom

 

AshishM
Fluorite | Level 6

Awesome!!! thanks exactly what I would need as a beginner. Have bookmarked all of them.

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 16. Read more here about why you should contribute and what is in it for you!

Submit your idea!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 18 replies
  • 7779 views
  • 6 likes
  • 3 in conversation