<jupyter_start><jupyter_text>Time series  Date and Time Data Types and Tools<jupyter_code>from datetime import datetime
now = datetime.now()
now
now.year, now.month, now.day
delta = datetime(2011, 1, 7) - datetime(2008, 6, 24, 8, 15)
delta
delta.days
delta.seconds
from datetime import timedelta
start = datetime(2011, 1, 7)
start + timedelta(12)
start - 2 * timedelta(12)<jupyter_output><empty_output><jupyter_text>Converting between string and datetime<jupyter_code>stamp = datetime(2011, 1, 3)
str(stamp)
stamp.strftime('%Y-%m-%d')
value = '2011-01-03'
datetime.strptime(value, '%Y-%m-%d')
datestrs = ['7/6/2011', '8/6/2011']
[datetime.strptime(x, '%m/%d/%Y') for x in datestrs]
from dateutil.parser import parse
parse('2011-01-03')
parse('Jan 31, 1997 10:45 PM')
parse('6/12/2011', dayfirst=True)
datestrs
import pandas as pd
pd.to_datetime(datestrs)
idx = pd.to_datetime(datestrs + [None])
idx
pd.isnull(pd.to_datetime(datestrs + [None]))<jupyter_output><empty_output><jupyter_text>Time Series Basics<jupyter_code>from pandas import Series, DataFrame
import numpy as np
dates = [datetime(2011, 1, 2), datetime(2011, 1, 5), datetime(2011, 1, 7),
         datetime(2011, 1, 8), datetime(2011, 1, 10), datetime(2011, 1, 12)]
ts = Series(np.random.randn(6), index=dates)
ts
type(ts)
ts + ts[::2]
ts.index.dtype
ts.index[0]<jupyter_output><empty_output><jupyter_text>Indexing, selection, subsetting<jupyter_code>ts['1/10/2011']
ts['20110110']
longer_ts = Series(np.random.randn(1000),
                   index=pd.date_range('1/1/2000', periods=1000))
longer_ts
longer_ts['2001']
longer_ts['2001-05']
ts[datetime(2011, 1, 7):]
ts
ts['1/6/2011':'1/11/2011']
ts.truncate(after='1/9/2011')
dates = pd.date_range('1/1/2000', periods=100, freq='W-WED')
long_df = DataFrame(np.random.randn(100, 4),
                    index=dates,
                    columns=['Colorado', 'Texas', 'New York', 'Ohio'])
long_df.ix['5-2001']<jupyter_output><empty_output><jupyter_text>Time series with duplicate indices<jupyter_code>dates = pd.DatetimeIndex(['1/1/2000', '1/2/2000', '1/2/2000', '1/2/2000',
                          '1/3/2000'])
dup_ts = Series(np.arange(5), index=dates)
dup_ts
dup_ts.index.is_unique
dup_ts['1/3/2000']
dup_ts['1/2/2000']
grouped = dup_ts.groupby(level=0)
grouped.mean()
grouped.count()<jupyter_output><empty_output><jupyter_text>Date ranges, Frequencies, and Shifting<jupyter_code>ts
ts.resample('D')<jupyter_output><empty_output><jupyter_text>Generating date ranges<jupyter_code>index = pd.date_range('4/1/2012', '6/1/2012')
index
pd.date_range(start='4/1/2012', periods=20)
pd.date_range(end='6/1/2012', periods=20)
pd.date_range('1/1/2000', '12/1/2000', freq='BM')
pd.date_range('5/2/2012 12:56:31', periods=5)
pd.date_range('5/2/2012 12:56:31', periods=5, normalize=True)<jupyter_output><empty_output><jupyter_text>Frequencies and Date Offsets<jupyter_code>from pandas.tseries.offsets import Hour, Minute
hour = Hour()
hour
four_hours = Hour(4)
four_hours
pd.date_range('1/1/2000', '1/3/2000 23:59', freq='4h')
Hour(2) + Minute(30)
pd.date_range('1/1/2000', periods=10, freq='1h30min')<jupyter_output><empty_output><jupyter_text>Week of month dates<jupyter_code>rng = pd.date_range('1/1/2012', '9/1/2012', freq='WOM-3FRI')
list(rng)<jupyter_output><empty_output><jupyter_text>Shifting (leading and lagging) data<jupyter_code>ts = Series(np.random.randn(4),
            index=pd.date_range('1/1/2000', periods=4, freq='M'))
ts
ts.shift(2)
ts.shift(-2)
ts / ts.shift(1) - 1
ts.shift(2, freq='M')
ts.shift(3, freq='D')
ts.shift(1, freq='3D')
ts.shift(1, freq='90T')<jupyter_output><empty_output><jupyter_text>Shifting dates with offsets<jupyter_code>from pandas.tseries.offsets import Day, MonthEnd
now = datetime(2011, 11, 17)
now + 3 * Day()
now + MonthEnd()
now + MonthEnd(2)
offset = MonthEnd()
offset.rollforward(now)
offset.rollback(now)
ts = Series(np.random.randn(20),
            index=pd.date_range('1/15/2000', periods=20, freq='4d'))
ts.groupby(offset.rollforward).mean()
ts.resample('M').mean()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1785626080513707, 'fraction_numerical': 0.09829587552482094, 'mean_word_length': 5.911262798634812, 'pattern_counts': {'":': 0, '<': 45, '<?xml version=': 0, '>': 45, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '11', 'dir': 'jupyter-structured-clean-dedup', 'id': '640600', 'license': 'MIT', 'n_tokens_mistral': 1890, 'n_tokens_neox': 1629, 'n_words': 281, 'path': 'Python_for_data_analysis/Chapter_10/Chapter_10_part_01.ipynb', 'repo_name': 'ALEXKIRNAS/DataScience', 'size': '4049'}