Skip to content

Commit 9d85b22

Browse files
FHaasejreback
authored andcommitted
Fix PEP-8 issues in visualization.rst (#23898)
Signed-off-by: Fabian Haase <[email protected]>
1 parent 18b68c7 commit 9d85b22

File tree

1 file changed

+69
-57
lines changed

1 file changed

+69
-57
lines changed

doc/source/timeseries.rst

+69-57
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,12 @@
44
.. ipython:: python
55
:suppress:
66
7-
import datetime
87
import numpy as np
98
import pandas as pd
10-
from pandas import offsets
9+
1110
np.random.seed(123456)
12-
randn = np.random.randn
13-
randint = np.random.randint
1411
np.set_printoptions(precision=4, suppress=True)
15-
pd.options.display.max_rows=15
16-
import dateutil
17-
import pytz
18-
from dateutil.relativedelta import relativedelta
12+
pd.options.display.max_rows = 15
1913
2014
********************************
2115
Time Series / Date functionality
@@ -32,7 +26,10 @@ Parsing time series information from various sources and formats
3226

3327
.. ipython:: python
3428
35-
dti = pd.to_datetime(['1/1/2018', np.datetime64('2018-01-01'), datetime.datetime(2018, 1, 1)])
29+
import datetime
30+
31+
dti = pd.to_datetime(['1/1/2018', np.datetime64('2018-01-01'),
32+
datetime.datetime(2018, 1, 1)])
3633
dti
3734
3835
Generate sequences of fixed-frequency dates and time spans
@@ -165,7 +162,9 @@ and :class:`PeriodIndex` respectively.
165162

166163
.. ipython:: python
167164
168-
dates = [pd.Timestamp('2012-05-01'), pd.Timestamp('2012-05-02'), pd.Timestamp('2012-05-03')]
165+
dates = [pd.Timestamp('2012-05-01'),
166+
pd.Timestamp('2012-05-02'),
167+
pd.Timestamp('2012-05-03')]
169168
ts = pd.Series(np.random.randn(3), dates)
170169
171170
type(ts.index)
@@ -329,7 +328,7 @@ which can be specified. These are computed from the starting point specified by
329328
1349979305, 1350065705], unit='s')
330329
331330
pd.to_datetime([1349720105100, 1349720105200, 1349720105300,
332-
1349720105400, 1349720105500 ], unit='ms')
331+
1349720105400, 1349720105500], unit='ms')
333332
334333
.. note::
335334

@@ -402,7 +401,9 @@ To generate an index with timestamps, you can use either the ``DatetimeIndex`` o
402401

403402
.. ipython:: python
404403
405-
dates = [datetime.datetime(2012, 5, 1), datetime.datetime(2012, 5, 2), datetime.datetime(2012, 5, 3)]
404+
dates = [datetime.datetime(2012, 5, 1),
405+
datetime.datetime(2012, 5, 2),
406+
datetime.datetime(2012, 5, 3)]
406407
407408
# Note the frequency information
408409
index = pd.DatetimeIndex(dates)
@@ -585,9 +586,8 @@ would include matching times on an included date:
585586

586587
.. ipython:: python
587588
588-
dft = pd.DataFrame(randn(100000,1),
589-
columns=['A'],
590-
index=pd.date_range('20130101',periods=100000,freq='T'))
589+
dft = pd.DataFrame(np.random.randn(100000, 1), columns=['A'],
590+
index=pd.date_range('20130101', periods=100000, freq='T'))
591591
dft
592592
dft['2013']
593593
@@ -624,10 +624,9 @@ We are stopping on the included end-point as it is part of the index:
624624
625625
dft2 = pd.DataFrame(np.random.randn(20, 1),
626626
columns=['A'],
627-
index=pd.MultiIndex.from_product([pd.date_range('20130101',
628-
periods=10,
629-
freq='12H'),
630-
['a', 'b']]))
627+
index=pd.MultiIndex.from_product(
628+
[pd.date_range('20130101', periods=10, freq='12H'),
629+
['a', 'b']]))
631630
dft2
632631
dft2.loc['2013-01-05']
633632
idx = pd.IndexSlice
@@ -683,7 +682,7 @@ If the timestamp string is treated as a slice, it can be used to index ``DataFra
683682
.. ipython:: python
684683
685684
dft_minute = pd.DataFrame({'a': [1, 2, 3], 'b': [4, 5, 6]},
686-
index=series_minute.index)
685+
index=series_minute.index)
687686
dft_minute['2011-12-31 23']
688687
689688
@@ -695,18 +694,16 @@ If the timestamp string is treated as a slice, it can be used to index ``DataFra
695694

696695
.. ipython:: python
697696
698-
dft_minute.loc['2011-12-31 23:59']
697+
dft_minute.loc['2011-12-31 23:59']
699698
700699
Note also that ``DatetimeIndex`` resolution cannot be less precise than day.
701700

702701
.. ipython:: python
703702
704703
series_monthly = pd.Series([1, 2, 3],
705-
pd.DatetimeIndex(['2011-12',
706-
'2012-01',
707-
'2012-02']))
704+
pd.DatetimeIndex(['2011-12', '2012-01', '2012-02']))
708705
series_monthly.index.resolution
709-
series_monthly['2011-12'] # returns Series
706+
series_monthly['2011-12'] # returns Series
710707
711708
712709
Exact Indexing
@@ -718,13 +715,14 @@ These ``Timestamp`` and ``datetime`` objects have exact ``hours, minutes,`` and
718715

719716
.. ipython:: python
720717
721-
dft[datetime.datetime(2013, 1, 1):datetime.datetime(2013,2,28)]
718+
dft[datetime.datetime(2013, 1, 1):datetime.datetime(2013, 2, 28)]
722719
723720
With no defaults.
724721

725722
.. ipython:: python
726723
727-
dft[datetime.datetime(2013, 1, 1, 10, 12, 0):datetime.datetime(2013, 2, 28, 10, 12, 0)]
724+
dft[datetime.datetime(2013, 1, 1, 10, 12, 0):
725+
datetime.datetime(2013, 2, 28, 10, 12, 0)]
728726
729727
730728
Truncating & Fancy Indexing
@@ -1045,14 +1043,16 @@ As an interesting example, let's look at Egypt where a Friday-Saturday weekend i
10451043

10461044
.. ipython:: python
10471045
1048-
from pandas.tseries.offsets import CustomBusinessDay
10491046
weekmask_egypt = 'Sun Mon Tue Wed Thu'
10501047
10511048
# They also observe International Workers' Day so let's
10521049
# add that for a couple of years
10531050
1054-
holidays = ['2012-05-01', datetime.datetime(2013, 5, 1), np.datetime64('2014-05-01')]
1055-
bday_egypt = CustomBusinessDay(holidays=holidays, weekmask=weekmask_egypt)
1051+
holidays = ['2012-05-01',
1052+
datetime.datetime(2013, 5, 1),
1053+
np.datetime64('2014-05-01')]
1054+
bday_egypt = pd.offsets.CustomBusinessDay(holidays=holidays,
1055+
weekmask=weekmask_egypt)
10561056
dt = datetime.datetime(2013, 4, 30)
10571057
dt + 2 * bday_egypt
10581058
@@ -1062,7 +1062,8 @@ Let's map to the weekday names:
10621062
10631063
dts = pd.date_range(dt, periods=5, freq=bday_egypt)
10641064
1065-
pd.Series(dts.weekday, dts).map(pd.Series('Mon Tue Wed Thu Fri Sat Sun'.split()))
1065+
pd.Series(dts.weekday, dts).map(
1066+
pd.Series('Mon Tue Wed Thu Fri Sat Sun'.split()))
10661067
10671068
Holiday calendars can be used to provide the list of holidays. See the
10681069
:ref:`holiday calendar<timeseries.holiday>` section for more information.
@@ -1071,7 +1072,7 @@ Holiday calendars can be used to provide the list of holidays. See the
10711072
10721073
from pandas.tseries.holiday import USFederalHolidayCalendar
10731074
1074-
bday_us = CustomBusinessDay(calendar=USFederalHolidayCalendar())
1075+
bday_us = pd.offsets.CustomBusinessDay(calendar=USFederalHolidayCalendar())
10751076
10761077
# Friday before MLK Day
10771078
dt = datetime.datetime(2014, 1, 17)
@@ -1084,15 +1085,15 @@ in the usual way.
10841085

10851086
.. ipython:: python
10861087
1087-
from pandas.tseries.offsets import CustomBusinessMonthBegin
1088-
bmth_us = pd.offsets.CustomBusinessMonthBegin(calendar=USFederalHolidayCalendar())
1088+
bmth_us = pd.offsets.CustomBusinessMonthBegin(
1089+
calendar=USFederalHolidayCalendar())
10891090
10901091
# Skip new years
10911092
dt = datetime.datetime(2013, 12, 17)
10921093
dt + bmth_us
10931094
10941095
# Define date index with custom offset
1095-
pd.DatetimeIndex(start='20100101',end='20120101',freq=bmth_us)
1096+
pd.DatetimeIndex(start='20100101', end='20120101', freq=bmth_us)
10961097
10971098
.. note::
10981099

@@ -1231,7 +1232,8 @@ You can use keyword arguments supported by either ``BusinessHour`` and ``CustomB
12311232

12321233
.. ipython:: python
12331234
1234-
bhour_mon = pd.offsets.CustomBusinessHour(start='10:00', weekmask='Tue Wed Thu Fri')
1235+
bhour_mon = pd.offsets.CustomBusinessHour(start='10:00',
1236+
weekmask='Tue Wed Thu Fri')
12351237
12361238
# Monday is skipped because it's a holiday, business hour starts from 10:00
12371239
dt + bhour_mon * 2
@@ -1429,11 +1431,14 @@ An example of how holidays and holiday calendars are defined:
14291431
USMemorialDay,
14301432
Holiday('July 4th', month=7, day=4, observance=nearest_workday),
14311433
Holiday('Columbus Day', month=10, day=1,
1432-
offset=pd.DateOffset(weekday=MO(2))), #same as 2*Week(weekday=2)
1433-
]
1434+
offset=pd.DateOffset(weekday=MO(2)))]
1435+
14341436
cal = ExampleCalendar()
14351437
cal.holidays(datetime.datetime(2012, 1, 1), datetime.datetime(2012, 12, 31))
14361438
1439+
:hint:
1440+
**weekday=MO(2)** is same as **2 * Week(weekday=2)**
1441+
14371442
Using this calendar, creating an index or doing offset arithmetic skips weekends
14381443
and holidays (i.e., Memorial Day/July 4th). For example, the below defines
14391444
a custom business day offset using the ``ExampleCalendar``. Like any other offset,
@@ -1442,10 +1447,9 @@ or ``Timestamp`` objects.
14421447

14431448
.. ipython:: python
14441449
1445-
from pandas.tseries.offsets import CDay
14461450
pd.DatetimeIndex(start='7/1/2012', end='7/10/2012',
1447-
freq=CDay(calendar=cal)).to_pydatetime()
1448-
offset = CustomBusinessDay(calendar=cal)
1451+
freq=pd.offsets.CDay(calendar=cal)).to_pydatetime()
1452+
offset = pd.offsets.CustomBusinessDay(calendar=cal)
14491453
datetime.datetime(2012, 5, 25) + offset
14501454
datetime.datetime(2012, 7, 3) + offset
14511455
datetime.datetime(2012, 7, 3) + 2 * offset
@@ -1532,7 +1536,7 @@ calls ``reindex``.
15321536
.. ipython:: python
15331537
15341538
dr = pd.date_range('1/1/2010', periods=3, freq=3 * pd.offsets.BDay())
1535-
ts = pd.Series(randn(3), index=dr)
1539+
ts = pd.Series(np.random.randn(3), index=dr)
15361540
ts
15371541
ts.asfreq(pd.offsets.BDay())
15381542
@@ -1626,7 +1630,7 @@ labels.
16261630

16271631
.. ipython:: python
16281632
1629-
ts.resample('5Min').mean() # by default label='left'
1633+
ts.resample('5Min').mean() # by default label='left'
16301634
16311635
ts.resample('5Min', label='left').mean()
16321636
@@ -1739,7 +1743,7 @@ We can select a specific column or columns using standard getitem.
17391743
17401744
r['A'].mean()
17411745
1742-
r[['A','B']].mean()
1746+
r[['A', 'B']].mean()
17431747
17441748
You can pass a list or dict of functions to do aggregation with, outputting a ``DataFrame``:
17451749

@@ -1760,21 +1764,21 @@ columns of a ``DataFrame``:
17601764
.. ipython:: python
17611765
:okexcept:
17621766
1763-
r.agg({'A' : np.sum,
1764-
'B' : lambda x: np.std(x, ddof=1)})
1767+
r.agg({'A': np.sum,
1768+
'B': lambda x: np.std(x, ddof=1)})
17651769
17661770
The function names can also be strings. In order for a string to be valid it
17671771
must be implemented on the resampled object:
17681772

17691773
.. ipython:: python
17701774
1771-
r.agg({'A' : 'sum', 'B' : 'std'})
1775+
r.agg({'A': 'sum', 'B': 'std'})
17721776
17731777
Furthermore, you can also specify multiple aggregation functions for each column separately.
17741778

17751779
.. ipython:: python
17761780
1777-
r.agg({'A' : ['sum','std'], 'B' : ['mean','std'] })
1781+
r.agg({'A': ['sum', 'std'], 'B': ['mean', 'std']})
17781782
17791783
17801784
If a ``DataFrame`` does not have a datetimelike index, but instead you want
@@ -1786,9 +1790,9 @@ to resample based on datetimelike column in the frame, it can passed to the
17861790
df = pd.DataFrame({'date': pd.date_range('2015-01-01', freq='W', periods=5),
17871791
'a': np.arange(5)},
17881792
index=pd.MultiIndex.from_arrays([
1789-
[1,2,3,4,5],
1790-
pd.date_range('2015-01-01', freq='W', periods=5)],
1791-
names=['v','d']))
1793+
[1, 2, 3, 4, 5],
1794+
pd.date_range('2015-01-01', freq='W', periods=5)],
1795+
names=['v', 'd']))
17921796
df
17931797
df.resample('M', on='date').sum()
17941798
@@ -1989,9 +1993,11 @@ Passing a string representing a lower frequency than ``PeriodIndex`` returns par
19891993
19901994
ps['2011']
19911995
1992-
dfp = pd.DataFrame(np.random.randn(600,1),
1996+
dfp = pd.DataFrame(np.random.randn(600, 1),
19931997
columns=['A'],
1994-
index=pd.period_range('2013-01-01 9:00', periods=600, freq='T'))
1998+
index=pd.period_range('2013-01-01 9:00',
1999+
periods=600,
2000+
freq='T'))
19952001
dfp
19962002
dfp['2013-01-01 10H']
19972003
@@ -2180,6 +2186,8 @@ time zones by starting with ``dateutil/``.
21802186

21812187
.. ipython:: python
21822188
2189+
import dateutil
2190+
21832191
# pytz
21842192
rng_pytz = pd.date_range('3/6/2012 00:00', periods=10, freq='D',
21852193
tz='Europe/London')
@@ -2201,6 +2209,8 @@ which gives you more control over which time zone is used:
22012209

22022210
.. ipython:: python
22032211
2212+
import pytz
2213+
22042214
# pytz
22052215
tz_pytz = pytz.timezone('Europe/London')
22062216
rng_pytz = pd.date_range('3/6/2012 00:00', periods=10, freq='D',
@@ -2299,7 +2309,8 @@ To remove timezone from tz-aware ``DatetimeIndex``, use ``tz_localize(None)`` or
22992309

23002310
.. ipython:: python
23012311
2302-
didx = pd.DatetimeIndex(start='2014-08-01 09:00', freq='H', periods=10, tz='US/Eastern')
2312+
didx = pd.DatetimeIndex(start='2014-08-01 09:00', freq='H',
2313+
periods=10, tz='US/Eastern')
23032314
didx
23042315
didx.tz_localize(None)
23052316
didx.tz_convert(None)
@@ -2352,7 +2363,8 @@ constructor as well as ``tz_localize``.
23522363
rng_hourly.tz_localize('US/Eastern', ambiguous=rng_hourly_dst).tolist()
23532364
rng_hourly.tz_localize('US/Eastern', ambiguous='NaT').tolist()
23542365
2355-
didx = pd.DatetimeIndex(start='2014-08-01 09:00', freq='H', periods=10, tz='US/Eastern')
2366+
didx = pd.DatetimeIndex(start='2014-08-01 09:00', freq='H',
2367+
periods=10, tz='US/Eastern')
23562368
didx
23572369
didx.tz_localize(None)
23582370
didx.tz_convert(None)
@@ -2403,14 +2415,14 @@ TZ Aware Dtypes
24032415

24042416
.. ipython:: python
24052417
2406-
s_naive = pd.Series(pd.date_range('20130101',periods=3))
2418+
s_naive = pd.Series(pd.date_range('20130101', periods=3))
24072419
s_naive
24082420
24092421
``Series/DatetimeIndex`` with a timezone **aware** value are represented with a dtype of ``datetime64[ns, tz]``.
24102422

24112423
.. ipython:: python
24122424
2413-
s_aware = pd.Series(pd.date_range('20130101',periods=3,tz='US/Eastern'))
2425+
s_aware = pd.Series(pd.date_range('20130101', periods=3, tz='US/Eastern'))
24142426
s_aware
24152427
24162428
Both of these ``Series`` can be manipulated via the ``.dt`` accessor, see :ref:`here <basics.dt_accessors>`.

0 commit comments

Comments
 (0)