So I know Python strings are immutable, but I have a string:
c[\'date\'] = \"20110104\"
Which I would like to convert to
c[\'da
s = '20110104'
def option_1():
return '-'.join([s[:4], s[4:6], s[6:]])
def option_1a():
return '-'.join((s[:4], s[4:6], s[6:]))
def option_2():
return '{}-{}-{}'.format(s[:4], s[4:6], s[6:])
def option_3():
return '%s-%s-%s' % (s[:4], s[4:6], s[6:])
def option_original():
return s[:4] + "-" + s[4:6] + "-" + s[6:]
Running %timeit
on each yields these results
So... pick the most readable because the performance improvements are marginal
I'm not usually the guy saying "use regex," but this is a good use-case for it:
import re
c['date']=re.sub(r'.*(\w{4})(\w{2})(\w{2}).*',r"\1-\2-\3",c['date'])
You are better off using string formatting than string concatenation
c['date'] = '{}-{}-{}'.format(c['date'][0:4], c['date'][4:6], c['date'][6:])
String concatenation is generally slower because as you said above strings are immutable.
Add hyphen to a series of strings to datetime
import datetime
for i in range (0,len(c.date)):
c.date[i] = datetime.datetime.strptime(c.date[i],'%Y%m%d').date().isoformat()
You could use .join()
to clean it up a little bit:
d = c['date']
'-'.join([d[:4], d[4:6], d[6:]])
Dates are first class objects in Python, with a rich interface for manipulating them. The library is datetime.
> import datetime
> datetime.datetime.strptime('20110503','%Y%m%d').date().isoformat()
'2011-05-03'
Don't reinvent the wheel!