Skip to content

Commit cfb5f0f

Browse files
committed
initial time and time zone doc. needs work. add intersphinx
1 parent 1d1bc0b commit cfb5f0f

File tree

3 files changed

+148
-1
lines changed

3 files changed

+148
-1
lines changed

docs/sphinx/source/conf.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ def __getattr__(cls, name):
5050
'sphinx.ext.autodoc',
5151
'sphinx.ext.mathjax',
5252
'sphinx.ext.viewcode',
53+
'sphinx.ext.intersphinx',
5354
'sphinx.ext.extlinks',
5455
'numpydoc',
5556
'sphinx.ext.autosummary',
@@ -77,7 +78,7 @@ def __getattr__(cls, name):
7778
# |version| and |release|, also used in various other places throughout the
7879
# built documents.
7980
# Get the version from the version file
80-
version_file = os.path.join(os.path.dirname(__file__),
81+
version_file = os.path.join(os.path.dirname(__file__),
8182
'../../../pvlib/version.py')
8283
with open(version_file, 'r') as f:
8384
exec(f.read())
@@ -301,3 +302,11 @@ def __getattr__(cls, name):
301302

302303
# If true, do not generate a @detailmenu in the "Top" node's menu.
303304
#texinfo_no_detailmenu = False
305+
306+
# Example configuration for intersphinx: refer to the Python standard library.
307+
intersphinx_mapping = {
308+
'python': ('https://docs.python.org/3.5/', None),
309+
'pandas': ('http://pandas.pydata.org/pandas-docs/stable/', None),
310+
'numpy': ('http://docs.scipy.org/doc/numpy/', None),
311+
}
312+

docs/sphinx/source/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ Contents
7272
whatsnew
7373
installation
7474
contributing
75+
timetimezones
7576
modules
7677
classes
7778
comparison_pvlib_matlab

docs/sphinx/source/timetimezones.rst

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
.. _timetimezones:
2+
3+
Time and time zones
4+
===================
5+
6+
Dealing with time and time zones can be a frustrating experience in any
7+
programming language and for any application. pvlib relies on :py:mod:`pandas`
8+
and
9+
pytz to handle time and time zones. Therefore, the vast majority of the
10+
information in this document applies to any time series analysis using
11+
pandas and is not specific to pvlib-python.
12+
13+
pvlib makes extensive use of pandas due to its excellent time series
14+
functionality. Take the time to become familiar with pandas' `Time
15+
Series / Date functionality page
16+
<http://pandas.pydata.org/pandas-docs/version/0.18.0/timeseries.html>`_.
17+
It is also worthwhile to become familiar with pure Python's
18+
:py:mod:`python:datetime` module, although we typically recommend
19+
using the corresponding pandas functionality where it exists.
20+
21+
.. ipython:: python
22+
23+
import datetime
24+
import numpy as np
25+
import pandas as pd
26+
import pytz
27+
28+
You can obtain a list of all valid time zone strings with
29+
pytz.all_timezones. Here, we print only every 20th time zone.
30+
31+
.. ipython:: python
32+
33+
len(pytz.all_timezones)
34+
pytz.all_timezones[::20]
35+
36+
:py:class:`pandas.Timestamp`'s and :py:class:`pandas.DatetimeIndex`'s
37+
can be created in many ways. Here
38+
we focus on the time zone issues surrounding them; see the pandas
39+
documentation for more information.
40+
41+
First, create a time zone naive pandas.Timestamp.
42+
43+
.. ipython:: python
44+
45+
pd.Timestamp('2015-1-1 00:00')
46+
47+
You can specify the time zone using the tz keyword argument or
48+
the tz_localize method of Timestamp
49+
and DatetimeIndex objects.
50+
51+
.. ipython:: python
52+
53+
pd.Timestamp('2015-1-1 00:00', tz='America/Denver')
54+
pd.Timestamp('2015-1-1 00:00').tz_localize('America/Denver')
55+
56+
Some time zones are aware of daylight savings time and some are not. For
57+
example the winter time results are the same for US/Mountain and MST,
58+
but the summer time results are not.
59+
60+
Note the UTC offset in winter...
61+
62+
.. ipython:: python
63+
64+
pd.Timestamp('2015-1-1 00:00').tz_localize('US/Mountain')
65+
pd.Timestamp('2015-1-1 00:00').tz_localize('MST')
66+
67+
vs. the UTC offset in summer...
68+
69+
.. ipython:: python
70+
71+
pd.Timestamp('2015-6-1 00:00').tz_localize('US/Mountain')
72+
pd.Timestamp('2015-6-1 00:00').tz_localize('MST')
73+
74+
pandas and pytz make this time zone handling possible because pandas
75+
stores all times as integer nanoseconds since January 1, 1970.
76+
Here is the pandas time representation of the integer 1.
77+
78+
.. ipython:: python
79+
80+
pd.Timestamp(1)
81+
82+
So if we specify times consistent with the specified time zone, pandas
83+
will use the same integer to represent them.
84+
85+
86+
.. ipython:: python
87+
88+
# US/Mountain
89+
pd.Timestamp('2015-6-1 01:00').tz_localize('US/Mountain').value
90+
91+
# MST
92+
pd.Timestamp('2015-6-1 00:00').tz_localize('MST').value
93+
94+
# UTC
95+
pd.Timestamp('2015-6-1 07:00').tz_localize('UTC').value
96+
97+
# UTC
98+
pd.Timestamp('2015-6-1 07:00').value
99+
100+
As stated above, pandas will assume UTC if you do not specify a time
101+
zone. This is dangerous, and we always recommend using using localized
102+
timeseries, even if it is UTC.
103+
104+
Timezones can also be specified with a fixed offset in minutes from UTC.
105+
106+
.. ipython:: python
107+
108+
pd.Timestamp('2015-1-1 00:00').tz_localize(pytz.FixedOffset(120))
109+
110+
You can also specify the fixed offset directly in the tz_localize
111+
method, however, be aware that this is not documented and that the
112+
offset must be in seconds, not minutes.
113+
114+
.. ipython:: python
115+
116+
pd.Timestamp('2015-1-1 00:00').tz_localize(7200)
117+
118+
Yet another way to specify a time zone with a fixed offset is by using
119+
the string formulation.
120+
121+
.. ipython:: python
122+
123+
pd.Timestamp('2015-1-1 00:00+0200')
124+
125+
pandas time objects can also be created from time zone aware or naive
126+
datetime.date or datetime.datetime objects. The behavior is generally as
127+
expected.
128+
129+
.. ipython:: python
130+
131+
# tz naive
132+
pd.Timestamp(datetime.datetime(2015,6,1,0))
133+
134+
# start is tz aware python datetime object
135+
start = pytz.timezone('US/Mountain').localize(datetime.datetime(2015, 6, 1, 0))
136+
pd.Timestamp(start)
137+

0 commit comments

Comments
 (0)