Python Forum

Full Version: df string manipulation before / after / when single digit pad zero
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Dear Python Experts,

I have some dates that I need to clearn up.

d = {'col1': ['1/25/84', '2/8/86'],}
dfdates = pd.DataFrame(data=d)
print(dfdates)
For the first date, I want to add a zero to all single digits before the first /
as well as a 19 to ahead of all 2 digits behind the last /.

In case of the second date, I would like to add a zero to all single digits before the first / and
a zero to all single digits behind the first /.

Can someone point me in the right direction which dataframe string manipulation function I should use?
xxx = re.sub(r'(\_a)?\.([^\.]*)$' , r'_suff.\2',"long.file.name.jpg")
xxxy = re.sub(r'^\d{1,2}\/\d{1,2}\/(\d{2}$)' , r'19\1',  "1/3/85")
print(xxxy)
OK I made some progress. However the 1/3/ are not printed but just 1985.
I just cant figure out why the beginning of the date is cut off.
Don't do this manually use a date library.
With dateutil can parse sting to datetime object.
>>> # pip install python-dateutil
>>> from dateutil.parser import parse
>>> 
>>> d = '1/25/84'
>>> d = parse(d)
>>> d
datetime.datetime(1984, 1, 25, 0, 0)
>>>
>>> print(d)
1984-01-25 00:00:00
>>> d.year
1984
Not that's is a datetime object it follow strftime() and strptime() Behavior
So can take out what needed, like %d is Day of the month as a zero-padded decimal number.
Example and also f-sting can take out format directly.
>>> d
datetime.datetime(1984, 1, 25, 0, 0)
>>> d.strftime('%d/%m/%Y')
'25/01/1984'
>>> 
>>> # f-string
>>> f'{d:%d/%m/%Y}'
'25/01/1984' 
Pandas has good date library build in,so you could probably do it there to.
Hi snippsat,
Many thanks for your reply.
Is there a way to try parsing it, if it doesnt parse, then it doesnt.
I have dates that contain strings or even months as strings in different languages.
It would be good if no exception is thrown, when the parsing fails.

    d=datetime(1984, 1, 'May', 0, 0)
    d.strftime(f'{d:%d/%m/%Y}')
    print(d)