官术网_书友最值得收藏!

Refactoring timezones

Next, we want to refactor the timezone based on our timezone:

We can refactor timezones by using the method given here:

import datetime 
import pytz

def refactor_timezone(x):
est = pytz.timezone('US/Eastern')
return x.astimezone(est)

Note that in the preceding code, I converted the timezone into the US/Eastern timezone. You can choose whatever timezone you like.

2.ow that our function is created, let's call it:

dfs['date'] = dfs['date'].apply(lambda x: refactor_timezone(x))

3.ext, we want to convert the day of the week variable into the name of the day, as in, SaturdaySunday, and so on. We can do that as shown here:

dfs['dayofweek'] = dfs['date'].apply(lambda x: x.weekday_name)
dfs['dayofweek'] = pd.Categorical(dfs['dayofweek'], categories=[
'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday',
'Saturday', 'Sunday'], ordered=True)

4.reat! Next, we do the same process for the time of the day. See the snippet given here:

dfs['timeofday'] = dfs['date'].apply(lambda x: x.hour + x.minute/60 + x.second/3600)

5.ext, we refactor the hour, the year integer, and the year fraction, respectively. First, refactor the hour as shown here:

dfs['hour'] = dfs['date'].apply(lambda x: x.hour)

6.efactor the year integer as shown here:

dfs['year_int'] = dfs['date'].apply(lambda x: x.year)

7.astly, refactor the year fraction as shown here:

dfs['year'] = dfs['date'].apply(lambda x: x.year + x.dayofyear/365.25)

8.aving done that, we can set the date to index and we will no longer require the original date field. So, we can remove that:

dfs.index = dfs['date']
del dfs['date']

Great! Good work so far. We have successfully executed our data transformation steps. If some of the steps were not clear, don't worry—we are going to deal with each of these phases in detail in upcoming chapters.

主站蜘蛛池模板: 九寨沟县| 鸡泽县| 子洲县| 弥勒县| 迭部县| 安顺市| 德化县| 竹山县| 荃湾区| 洛南县| 金山区| 同仁县| 谢通门县| 息烽县| 财经| 偏关县| 宜宾市| 大冶市| 那坡县| 三原县| 固原市| 高邮市| 平塘县| 报价| 天气| 浮山县| 鄂伦春自治旗| 南岸区| 台湾省| 新沂市| 榆中县| 钦州市| 策勒县| 肇庆市| 舞钢市| 凯里市| 简阳市| 西乌珠穆沁旗| 墨竹工卡县| 通江县| 任丘市|