Cum să utilizați date și ore cu pandas

Manipularea datelor de tip serie de timp în Python

Stefan Jansen

Founder & Lead Data Scientist at Applied Artificial Intelligence

Funcționalități pentru serii de date și ore

  • La bază: tipuri de date pentru informații de dată și oră
    • Obiecte pentru momente și perioade de timp
    • Atributele și metodele reflectă detalii temporale
  • Secvențe de date și perioade:
    • Coloane Series sau DataFrame
    • Index: transformă obiectul într-o serie de timp
  • Multe metode Series/DataFrame utilizează informațiile temporale din index pentru funcționalități de serii de timp
Manipularea datelor de tip serie de timp în Python

Bloc de bază: pd.Timestamp

import pandas as pd  # assumed imported going forward
from datetime import datetime  # To manually create dates

time_stamp = pd.Timestamp(datetime(2017, 1, 1))
pd.Timestamp('2017-01-01') == time_stamp
True # Understands dates as strings
time_stamp # type: pandas.tslib.Timestamp
Timestamp('2017-01-01 00:00:00')
Manipularea datelor de tip serie de timp în Python

Bloc de bază: pd.Timestamp

  • Obiectul Timestamp are numeroase atribute pentru informații temporale
time_stamp.year
2017
time_stamp.day_name()
'Sunday'
Manipularea datelor de tip serie de timp în Python

Mai multe blocuri: pd.Period & freq

period = pd.Period('2017-01')

period # default: month-end
Period('2017-01', 'M')
period.asfreq('D') # convert to daily
Period('2017-01-31', 'D')
period.to_timestamp().to_period('M')
Period('2017-01', 'M')

 

  • Obiectul Period are atributul freq pentru informații de frecvență

 

  • Conversia pd.Period() în pd.Timestamp() și invers
Manipularea datelor de tip serie de timp în Python

Mai multe blocuri: pd.Period & freq

period + 2
Period('2017-03', 'M')
pd.Timestamp('2017-01-31', 'M') + 1
Timestamp('2017-02-28 00:00:00', freq='M')
  • Informațiile de frecvență permit aritmetică de bază cu date
Manipularea datelor de tip serie de timp în Python

Secvențe de date și ore

  • pd.date_range: start, end, periods, freq
index = pd.date_range(start='2017-1-1', periods=12, freq='M')
index
DatetimeIndex(['2017-01-31', '2017-02-28', '2017-03-31', ...,
               '2017-09-30', '2017-10-31', '2017-11-30', '2017-12-31'],
              dtype='datetime64[ns]', freq='M')
  • pd.DateTimeIndex: secvență de obiecte Timestamp cu informații de frecvență
Manipularea datelor de tip serie de timp în Python

Secvențe de date și ore

index[0]
Timestamp('2017-01-31 00:00:00', freq='M')
index.to_period()
PeriodIndex(['2017-01', '2017-02', '2017-03', '2017-04', ..., 
             '2017-11', '2017-12'], dtype='period[M]', freq='M')
Manipularea datelor de tip serie de timp în Python

Crearea unei serii de timp: pd.DateTimeIndex

pd.DataFrame({'data': index}).info()
RangeIndex: 12 entries, 0 to 11
Data columns (total 1 columns):
data    12 non-null datetime64[ns]
dtypes: datetime64[ns](1)
Manipularea datelor de tip serie de timp în Python

Crearea unei serii de timp: pd.DateTimeIndex

  • np.random.random:
    • Numere aleatoare: [0,1]
    • 12 rânduri, 2 coloane
data = np.random.random((size=12,2))

pd.DataFrame(data=data, index=index).info()
DatetimeIndex: 12 entries, 2017-01-31 to 2017-12-31
Freq: M
Data columns (total 2 columns):
0    12 non-null float64
1    12 non-null float64
dtypes: float64(2)
Manipularea datelor de tip serie de timp în Python

Aliasuri de frecvență și informații temporale

ch1_1_v2 -Cum să utilizați date și ore cu pandas.036.png

Manipularea datelor de tip serie de timp în Python

Să exersăm!

Manipularea datelor de tip serie de timp în Python

Preparing Video For Download...