Amending datetime format while parsing from csv read - pandas
<p>I am reading a csv file (SimResults_Daily.csv) into pandas, that is structured as follows:</p>

<pre><code>#, Job_ID, Date/Time, value1, value2,
0, ID1,  05/01  24:00:00, 5, 6 
1, ID2,  05/02  24:00:00, 6, 15 
2, ID3,  05/03  24:00:00, 20, 21 
</code></pre>

<p>etc.
As the datetime format cannot be read by pandas parse_dates, I have figured out I can use the command: <code>str.replace('24:','00:')</code>.</p>

<p>My code currently is:</p>

<pre><code>dateparse = lambda x: pd.datetime.strptime(x, '%m-%d  %H:%M:%S')

df = pd.read_csv('SimResults_Daily.csv',
    skipinitialspace=True,
    date_parser=dateparse,
    parse_dates=['Date/Time'],
    index_col=['Date/Time'],
    usecols=['Job_ID',
    'Date/Time',
    'value1',
    'value2',
    header=0)
</code></pre>

<p>Where in the code should I implement the <code>str.replace</code> command?</p>

<p>You can use:</p>

<pre><code>import pandas as pd
import io

temp=u"""#,Job_ID,Date/Time,value1,value2,
0,ID1,05/01 24:00:00,5,6
1,ID2,05/02 24:00:00,6,15
2,ID3,05/03 24:00:00,20,21"""

dateparse = lambda x: pd.datetime.strptime(x.replace('24:','00:'), '%m/%d  %H:%M:%S')

#after testing replace io.StringIO(temp) to filename
df = pd.read_csv(io.StringIO(temp),
    skipinitialspace=True,
    date_parser=dateparse,
    parse_dates=['Date/Time'],
    index_col=['Date/Time'],
    usecols=['Job_ID', 'Date/Time', 'value1', 'value2'],
    header=0)

print (df)
           Job_ID  value1  value2
Date/Time                        
1900-05-01    ID1       5       6
1900-05-02    ID2       6      15
1900-05-03    ID3      20      21
</code></pre>

<hr>

<p>Another solution with double <code>replace</code> - <code>year</code> can be added also:</p>

<pre><code>dateparse = lambda x: x.replace('24:','00:').replace(' ','/1900 ')

df = pd.read_csv(io.StringIO(temp),
    skipinitialspace=True,
    date_parser=dateparse,
    parse_dates=['Date/Time'],
    index_col=['Date/Time'],
    usecols=['Job_ID', 'Date/Time', 'value1', 'value2'],
    header=0)

print (df)
           Job_ID  value1  value2
Date/Time                        
1900-05-01    ID1       5       6
1900-05-02    ID2       6      15
1900-05-03    ID3      20      21
</code></pre>

<hr>

<pre><code>dateparse = lambda x: x.replace('24:','00:').replace(' ','/2016 ')

df = pd.read_csv(io.StringIO(temp),
    skipinitialspace=True,
    date_parser=dateparse,
    parse_dates=['Date/Time'],
    index_col=['Date/Time'],
    usecols=['Job_ID', 'Date/Time', 'value1', 'value2'],
    header=0)

print (df)
           Job_ID  value1  value2
Date/Time                        
2016-05-01    ID1       5       6
2016-05-02    ID2       6      15
2016-05-03    ID3      20      21
</code></pre>

python date csv parsing pandas