Python Forum
df string manipulation before / after / when single digit pad zero
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
df string manipulation before / after / when single digit pad zero
#1
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?
Reply
#2
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.
Reply
#3
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.
Reply
#4
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)
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020