-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDate series process
63 lines (48 loc) · 2.4 KB
/
Date series process
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
1. Pick a slice in Panda time index
a.index #pandas.core.indexes.datetimes.DatetimeIndex
np.where(np.logical_and(a.index>'20190506',a.index<'20190508'))
# but remember, the value return is a multidimension array
2. datetime format array and using logical_or and logical_and
time = np.array([dt.datetime.strptime('{}-{}-{}T{}:{}'.format(int(data[i,0]),int(data[i,1]),int(data[i,2]),int(data[i,4]), \
int(data[i,5])),'%Y-%m-%dT%H:%M') for i in range(len(data))])
time_range = np.logical_or(np.logical_and(time>dt.datetime(2019,1,2),time<dt.datetime(2019,1,12,12)), \
np.logical_and(time>dt.datetime(2019,1,30,17),time<dt.datetime(2019,2,10,8)))
3. <change the shape of array>
Flatten
reshape
Numpy Arrays: Concatenating, Flattening and Adding Dimensions
https://www.python-course.eu/numpy_changing_dimensions.php
4. Concatenate two array
https://cmdlinetips.com/2018/04/how-to-concatenate-arrays-in-numpy/
5: How to plot the pandas datetime index using matplotlib.pyplot
See http://pandas.pydata.org/pandas-docs/stable/whatsnew.html#v0-21-0-october-27-2017 / #17710
and https://matplotlib.org/faq/howto_faq.html#plot-numpy-datetime64-values
# add this before plot.
from pandas.tseries import converter
converter.register()
6. convert string to datetime: inclding one function convert pandas to datetime
#https://chrisalbon.com/python/basics/strings_to_datetime/
from dateutil.parser import parse: directly parse date string
pd.to_datetime(df['date'])
7. calculate the delta time:
time = np.append(time, (parse(meta['DATE-OBS'])-time_0)/datetime.timedelta(seconds=1))
8. Timezone aware and unware issu of datetime
---Method 1.
from datetime import datetime
from datetime import timezone
dt = datetime.now()
dt.replace(tzinfo=timezone.utc)
----Method 2.
import datetime
import pytz
unaware = datetime.datetime(2011, 8, 15, 8, 15, 12, 0)
aware = datetime.datetime(2011, 8, 15, 8, 15, 12, 0, pytz.UTC) # this is a way method 3.
now_aware = pytz.utc.localize(unaware) # method 2
assert aware == now_aware
print(dt.replace(tzinfo=timezone.utc).isoformat())
'2017-01-12T22:11:31+00:00'
-----Case 3: pandas read and localized it
location = pd.read_csv(file, sep = ' ', skiprows=1, index_col=0, header = None,parse_dates=True)
import pytz
location = location.rename_axis( 'Time')
location.index = location.index.tz_localize(pytz.UTC)