1

I have a problem of the following nature. After reading the data from the csv file to the pandas DataFrame, I have the date in the first column. The format of this date is six characters "yymmdd" (int64). Unfortunately, all attempts to convert to the "yyyy-mm-dd" format have failed.

From the input "171207" it gets the value "1970-01-01 00: 00: 00.000171207". None of the functions I tested support the YY-MM-DD format. (Python 3.9) Asking for suggestions.

Thanks in advance!

FObersteiner
  • 22,500
  • 8
  • 42
  • 72
  • Use `pd.to_datetime` with `format='%y%m%d'` – FObersteiner Dec 25 '21 at 10:12
  • So I started my enhancement with conversion from this formula. It works with a single action. But it is not enough to convert the entire column from the csv file. Below are the results. – gregorylenartowicz Dec 25 '21 at 15:31
  • Basically, it should work [like this](https://stackoverflow.com/q/17134716/10197418), only that your input data type is int64 - which is no problem because pd.to_datetime will auto-comvert to string if you specify the format as I commented above. – FObersteiner Dec 25 '21 at 16:33
  • 2
    Yes you are right. Now understood, corrected and it works. Thank you very much for your help, patience and understanding. Greetings – gregorylenartowicz Dec 26 '21 at 15:39

1 Answers1

0

You can handle the Problem by Hand.

# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility

import re

regex = r"(\d{2})(\d{2})(\d{2})"

test_str = "171223"

matches = re.finditer(regex, test_str, re.MULTILINE)

for matchNum, match in enumerate(matches, start=1):
    
    print ("Match {matchNum} was found at {start}-{end}: {match}".format(matchNum = matchNum, start = match.start(), end = match.end(), match = match.group()))
    
    for groupNum in range(0, len(match.groups())):
        groupNum = groupNum + 1
        
        print ("Group {groupNum} found at {start}-{end}: {group}".format(groupNum = groupNum, start = match.start(groupNum), end = match.end(groupNum), group = match.group(groupNum)))

# Note: for Python 2.7 compatibility, use ur"" to prefix the regex and u"" to prefix the test string and substitution.

For more info of Regex use http://regex101.com

Enigmativity
  • 113,464
  • 11
  • 89
  • 172
Denis Kohl
  • 739
  • 8
  • 13
  • Thank you for taking up the topic. However, the result of the applied solution did not meet my expectations or I am not able to use it properly. Maybe there are still some options for converting the "yy-mm-dd" format to "yyyy-mm-dd". I count on your support. – gregorylenartowicz Dec 25 '21 at 10:06