2017-11-07 1 views
1

Ich habe eine große txt-Datei (~ 300 MB) mit Werten und Form wie:große Datetime-Objekte Pandas aus dem Speicher verursacht

df= pd.read_csv('file.txt') 
df.head() 

    <Base> <DTYYYYMMDD> <TIME> <p1> <p2> <p3> <p4> <q> 
36 x  20010102 235700 0.5622 0.5622 0.5622 0.5622 4 
37 x  20010102 235800 0.5622 0.5622 0.5622 0.5622 4 
38 x  20010102 235900 0.5622 0.5622 0.5622 0.5622 4 
39 x  20010103 0  0.5618 0.5618 0.5618 0.5618 4 
40 x  20010103 300  0.5622 0.5622 0.5622 0.5622 4 
41 x  20010103 500  0.5622 0.5622 0.5622 0.5622 4 

df.shape() 

(5560000, 8) 

Ich versuche nur, den Wert p4 und zeichnen Sie die Anzahl von Vorkommnissen zu bekommen in jedem Jahr unter anderem. Dazu versuche ich zum ersten DTYYYYMMDD und Zeitfelder in Strings (sie lesen, wie ganze Zahlen von Textdatei), dann wandeln sie in Datetime konvertieren wie folgt:

datestr = df['<DTYYYYMMDD>'].apply(lambda x: str(x)) 
timestr = df['<TIME>'].apply(lambda x: str(x)) 
zeros = timestr.apply(lambda x: '0' * (6- len(x))) 
timestr = zeros + timestr 
dtstr = datestr + timestr 
p4_df = df['<p4>'] 
dt_datetime = pd.to_datetime(dtstr, format = '%Y%m%d%H%M%S') 
p4_df.index = dt_datetime 

Jetzt versuche ich nur noch das Datum Teil zu erhalten getrennt, damit ich das gruppieren kann und die Vorkommen zählen kann. Ich muss den vollen Datum-Zeit-Index auch behalten, da ich das in anderen Berechnungen brauche.

p4_df['Date'] = dt_datetime.apply(lambda x: x.date()) 
to_plot = p4_df.groupby(['Date'])['<p4>'].count() 
to_plot.plot() 

Ich bekomme einen Speicherfehler auf dt_datetime.apply Zeile. Ich habe versucht, stattdessen aber immer noch für den Fehler zu verwenden:

p4_df['Date'] = pd.to_datetime(datestr, format = '%Y%m%d') 

Haben Sie irgendwelche Vorschläge, um den Code effizienter zu machen?

Antwort

1

Sie müssen astype für convert in Strings und dann Nullen hinzufügen, indem zfill:

dtstr = df['<DTYYYYMMDD>'].astype(str) + df['<TIME>'].astype(str).str.zfill(6) 
df.index = pd.to_datetime(dtstr, format = '%Y%m%d%H%M%S') 
print (df) 
        <Base> <DTYYYYMMDD> <TIME> <p1> <p2> <p3> \ 
2001-01-02 23:57:00  x  20010102 235700 0.5622 0.5622 0.5622 
2001-01-02 23:58:00  x  20010102 235800 0.5622 0.5622 0.5622 
2001-01-02 23:59:00  x  20010102 235900 0.5622 0.5622 0.5622 
2001-01-03 00:00:00  x  20010103  0 0.5618 0.5618 0.5618 
2001-01-03 00:03:00  x  20010103  300 0.5622 0.5622 0.5622 
2001-01-03 00:05:00  x  20010103  500 0.5622 0.5622 0.5622 

         <p4> <q> 
2001-01-02 23:57:00 0.5622 4 
2001-01-02 23:58:00 0.5622 4 
2001-01-02 23:59:00 0.5622 4 
2001-01-03 00:00:00 0.5618 4 
2001-01-03 00:03:00 0.5622 4 
2001-01-03 00:05:00 0.5622 4 

Eine weitere Leistung besser werden, wenn die Verwendung DatetimeIndex.floor für dates:

#if dont need omit NaNs use size instaed count 
to_plot = df.groupby(df.index.floor('D'))['<p4>'].count() 
to_plot.plot() 

Oder verwenden date:

to_plot = df.groupby(df.index.date)['<p4>'].count() 
to_plot.plot() 

Eine weitere Idee ist nur <DTYYYYMMDD> verwenden, dann konvertiert string ist nicht notwendig:

df.index = pd.to_datetime(df['<DTYYYYMMDD>'], format = '%Y%m%d') 
print (df) 
      <Base> <DTYYYYMMDD> <TIME> <p1> <p2> <p3> <p4> <q> 
<DTYYYYMMDD>                 
2001-01-02  x  20010102 235700 0.5622 0.5622 0.5622 0.5622 4 
2001-01-02  x  20010102 235800 0.5622 0.5622 0.5622 0.5622 4 
2001-01-02  x  20010102 235900 0.5622 0.5622 0.5622 0.5622 4 
2001-01-03  x  20010103  0 0.5618 0.5618 0.5618 0.5618 4 
2001-01-03  x  20010103  300 0.5622 0.5622 0.5622 0.5622 4 
2001-01-03  x  20010103  500 0.5622 0.5622 0.5622 0.5622 4 

to_plot = df.groupby(level=0)['<p4>'].count() 
print (to_plot) 
<DTYYYYMMDD> 
2001-01-02 3 
2001-01-03 3 
Name: <p4>, dtype: int64 

EDIT1: Bessere Leistung erstes Aggregat von Strings sein sollte und dann in Datetime kleinen aggregierte Ausgabe konvertieren:

to_plot = df.groupby('<DTYYYYMMDD>')['<p4>'].count() 
to_plot.index = pd.to_datetime(to_plot.index, format = '%Y%m%d') 
print (to_plot) 
<DTYYYYMMDD> 
2001-01-02 3 
2001-01-03 3 
Name: <p4>, dtype: int64 

EDIT2:

Wenn Bedarf Verwendung Variablen in einem anderen Code:

datestr = df['<DTYYYYMMDD>'].astype(str) 
timestr = df['<TIME>'].astype(str).str.zfill(6) 

dtstr = datestr + timestr 

p4_df = df['<p4>'] 
dt_datetime = pd.to_datetime(dtstr, format = '%Y%m%d%H%M%S') 
p4_df.index = dt_datetime 

p4_df['Date'] = dt_datetime.date() 
to_plot = p4_df.groupby(['Date'])['<p4>'].count() 
to_plot.plot() 
+1

Danke, viele gute Punkte drin! – dayum

Verwandte Themen