lab06-checkpoint.ipynb 90.6 KB
Newer Older
root's avatar
root committed
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Lab 06 - Data Preprocessing 1\n",
    "\n",
    "Name: Mitch Gavars and Meg Koczur\n",
    "\n",
    "Class: CSCI 349 \n",
    "\n",
    "Semester: 2021 Spring\n",
    "\n",
    "Insructor: Brian King"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "import matplotlib.pyplot as plt\n",
    "import seaborn as sns"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### 1) [P] Use pandas to read in your CSV data file you downloaded above. (You should have placed in your data directory.) Call the data frame df_temps. Read in the entire dataset, however, peek at the dataset first. You'll notice 16 rows of metadata. Ignore the first 16 rows (HINT: Use the skiprows= option!)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "path = '../data/faa_hourly-KIPT_20000101-20201231_raw.csv'\n",
    "df_temps = pd.read_csv(r'../data/faa_hourly-KIPT_20000101-20201231_raw.csv', skiprows=16)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### 2) Report the general structure of the data frame using df_temps.info(). You should notice that almost every variable was read in as a plan object data type. You have a lot of work to do!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 181943 entries, 0 to 181942\n",
      "Data columns (total 14 columns):\n",
      " #   Column                         Non-Null Count   Dtype  \n",
      "---  ------                         --------------   -----  \n",
      " 0   Date/Time (GMT)                181943 non-null  object \n",
      " 1   Number of Observations (n/a)   181943 non-null  int64  \n",
      " 2   Average Temp (F)               180938 non-null  float64\n",
      " 3   Max Temp (F)                   180938 non-null  float64\n",
      " 4   Min Temp (F)                   180938 non-null  float64\n",
      " 5   Average Dewpoint Temp (F)      180816 non-null  float64\n",
      " 6   1 Hour Precip (in)             30294 non-null   float64\n",
      " 7   Max Wind Gust (mph)            24708 non-null   float64\n",
      " 8   Average Relative Humidity (%)  177114 non-null  float64\n",
      " 9   Average Wind Speed (mph)       181394 non-null  float64\n",
      " 10  Average Station Pressure (mb)  181647 non-null  float64\n",
      " 11  Average Wind Direction (deg)   148822 non-null  float64\n",
      " 12  Max Wind Speed (mph)           181394 non-null  float64\n",
      " 13  Unnamed: 13                    0 non-null       float64\n",
      "dtypes: float64(12), int64(1), object(1)\n",
      "memory usage: 19.4+ MB\n"
     ]
    }
   ],
   "source": [
    "df_temps.info()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### 3) [P] Read about the memory_usage() method of pandas data frames. Then, report the total memory in bytes for each variable of df_temps. Set the parameter deep=True, to get the most accurate assessment of your total memory usage. (NOTE – this could take a bit of time to return an answer.)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index                                 128\n",
       "Date/Time (GMT)                  13827668\n",
       "Number of Observations (n/a)      1455544\n",
       "Average Temp (F)                  1455544\n",
       "Max Temp (F)                      1455544\n",
       "Min Temp (F)                      1455544\n",
       "Average Dewpoint Temp (F)         1455544\n",
       "1 Hour Precip (in)                1455544\n",
       "Max Wind Gust (mph)               1455544\n",
       "Average Relative Humidity (%)     1455544\n",
       "Average Wind Speed (mph)          1455544\n",
       "Average Station Pressure (mb)     1455544\n",
       "Average Wind Direction (deg)      1455544\n",
       "Max Wind Speed (mph)              1455544\n",
       "Unnamed: 13                       1455544\n",
       "dtype: int64"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_temps.memory_usage(deep=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### 4) [P] Report the total memory required for the data frame in MB. (Just sum the previous answer.) You should get an answer showing over a hundred megabytes! Also, store the total as a variable called original_memory. We're going to compare memory after we're done."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "32749868"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "original_memory = df_temps.memory_usage(deep=True).sum()\n",
    "original_memory"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### 5) [P] You have a rather annoying extra column that was read in in the last column position. (Look closely at the output of `info()` above!) You should always confirm that it's garbage before deleting it. Write the single line of code that reports the count of valid values in the last column (HINT: count())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_temps['Unnamed: 13'].count()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### 6) [P] Drop that last column from df_temps."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_temps = df_temps.drop(columns = 'Unnamed: 13')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### 7) [M] Look over the data type column in the info() output. ALWAYS pay attention to the types of each variable. In particular, pay attention to the variables that are read in as \"object\" type. This implies that pandas did not have enough confidence to convert the type itself, and you need to do it. Are there any object types? If so what? What format are the data in that column(s)?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0         2000-01-01 00:00:00\n",
       "1         2000-01-01 01:00:00\n",
       "2         2000-01-01 02:00:00\n",
       "3         2000-01-01 03:00:00\n",
       "4         2000-01-01 04:00:00\n",
       "                 ...         \n",
       "181938    2020-12-31 19:00:00\n",
       "181939    2020-12-31 20:00:00\n",
       "181940    2020-12-31 21:00:00\n",
       "181941    2020-12-31 22:00:00\n",
       "181942    2020-12-31 23:00:00\n",
       "Name: Date/Time (GMT), Length: 181943, dtype: object"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_temps['Date/Time (GMT)']"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "There is one column of objects 'Date/Time (GMT)'. It got put in as an object because each observation of data is in the form 'year-month-day hour:minute:second' so it is not an observation that can fit into one type easily. "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### 8) [P] How many NaN values are in each variable? (NOTE: Leave the NaN fields alone! The fact that they are missing is IMPORTANT! And, leave the date/time variable in the first column alone.)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Number of Observations (n/a)          0\n",
       "Average Temp (F)                   1005\n",
       "Max Temp (F)                       1005\n",
       "Min Temp (F)                       1005\n",
       "Average Dewpoint Temp (F)          1127\n",
       "1 Hour Precip (in)               151649\n",
       "Max Wind Gust (mph)              157235\n",
       "Average Relative Humidity (%)      4829\n",
       "Average Wind Speed (mph)            549\n",
       "Average Station Pressure (mb)       296\n",
       "Average Wind Direction (deg)      33121\n",
       "Max Wind Speed (mph)                549\n",
       "dtype: int64"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cols = df_temps.columns\n",
    "df_temps[ cols[1:] ].isna().sum()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### 9) [P] Report the NaN output as a percentage of the total number of values that are missing for each variable"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "NaN outputs per variable (in %): \n",
      "\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "Number of Observations (n/a)       0.000000\n",
       "Average Temp (F)                   0.555439\n",
       "Max Temp (F)                       0.555439\n",
       "Min Temp (F)                       0.555439\n",
       "Average Dewpoint Temp (F)          0.623286\n",
       "1 Hour Precip (in)               500.590876\n",
       "Max Wind Gust (mph)              636.372835\n",
       "Average Relative Humidity (%)      2.726493\n",
       "Average Wind Speed (mph)           0.302656\n",
       "Average Station Pressure (mb)      0.162953\n",
       "Average Wind Direction (deg)      22.255446\n",
       "Max Wind Speed (mph)               0.302656\n",
       "dtype: float64"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "print(\"NaN outputs per variable (in %): \\n\")\n",
    "100 * df_temps[ cols[1:] ].isna().sum() / df_temps[ cols[1:] ].count()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### 10) [PM] Report the number of observations that are complete, meaning, they have NO missing variable in the observation. Report this as a raw number and as a percentage of the total number of observations. Then, clearly state why this is NOT a problem to be concerned about for this particular dataset. (HINT: Which variable(s) contain most of the missing data and why?)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5552 complete observations\n"
     ]
    }
   ],
   "source": [
    "complete_obs = 0\n",
    "for index, row in df_temps[ cols[1:] ].iterrows():\n",
    "    if row.isna().sum() == 0:\n",
    "        complete_obs += 1\n",
    "        \n",
    "print(complete_obs, 'complete observations')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Percentage of complete observations in the whole dataset is:  3.051505141720209 %\n"
     ]
    }
   ],
   "source": [
    "r,c = df_temps.shape\n",
    "\n",
    "print(\"Percentage of complete observations in the whole dataset is: \", 100 * complete_obs/r, \"%\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Even though there are over 180,000 observations, it is okay to only have this many complete observations because the time intervals were every hour over 20 years. Since we have 5552 complete observations, if we divide that by 20 and again by 365, we get about .76. This means we got about 3 observations every 4 days for over 20 years. That is completely acceptable. \n",
    "\n",
    "Another reason is because the 1 Hour precipitation and Max wind gust were the two variables with the least aount of observations by a large margin, so we do not expect that many observations anyway."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### 11) [P] Look over your data types. By default, most of the time pandas will convert your integer types to a 64-bit integer, and floating point types will use double precision numbers. You can do far better. Read over the pd.to_numeric() function. Did you notice the parameter called downcast? Go back and read about this parameter. Downcast your types accordingly. Then look over the output of info(), and report your latest memory usage in MB."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 181943 entries, 0 to 181942\n",
      "Data columns (total 13 columns):\n",
      " #   Column                         Non-Null Count   Dtype  \n",
      "---  ------                         --------------   -----  \n",
      " 0   Date/Time (GMT)                181943 non-null  object \n",
      " 1   Number of Observations (n/a)   181943 non-null  int64  \n",
      " 2   Average Temp (F)               180938 non-null  float64\n",
      " 3   Max Temp (F)                   180938 non-null  float64\n",
      " 4   Min Temp (F)                   180938 non-null  float64\n",
      " 5   Average Dewpoint Temp (F)      180816 non-null  float64\n",
      " 6   1 Hour Precip (in)             30294 non-null   float64\n",
      " 7   Max Wind Gust (mph)            24708 non-null   float64\n",
      " 8   Average Relative Humidity (%)  177114 non-null  float64\n",
      " 9   Average Wind Speed (mph)       181394 non-null  float64\n",
      " 10  Average Station Pressure (mb)  181647 non-null  float64\n",
      " 11  Average Wind Direction (deg)   148822 non-null  float64\n",
      " 12  Max Wind Speed (mph)           181394 non-null  float64\n",
      "dtypes: float64(11), int64(1), object(1)\n",
      "memory usage: 18.0+ MB\n"
     ]
    }
   ],
   "source": [
    "df_temps.info()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Latest memory usage is:  22561060 \n",
      "\n",
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 181943 entries, 0 to 181942\n",
      "Data columns (total 13 columns):\n",
      " #   Column                         Non-Null Count   Dtype  \n",
      "---  ------                         --------------   -----  \n",
      " 0   Date/Time (GMT)                181943 non-null  object \n",
      " 1   Number of Observations (n/a)   181943 non-null  float32\n",
      " 2   Average Temp (F)               180938 non-null  float32\n",
      " 3   Max Temp (F)                   180938 non-null  float32\n",
      " 4   Min Temp (F)                   180938 non-null  float32\n",
      " 5   Average Dewpoint Temp (F)      180816 non-null  float32\n",
      " 6   1 Hour Precip (in)             30294 non-null   float32\n",
      " 7   Max Wind Gust (mph)            24708 non-null   float32\n",
      " 8   Average Relative Humidity (%)  177114 non-null  float32\n",
      " 9   Average Wind Speed (mph)       181394 non-null  float32\n",
      " 10  Average Station Pressure (mb)  181647 non-null  float32\n",
      " 11  Average Wind Direction (deg)   148822 non-null  float32\n",
      " 12  Max Wind Speed (mph)           181394 non-null  float32\n",
      "dtypes: float32(12), object(1)\n",
      "memory usage: 9.7+ MB\n"
     ]
    }
   ],
   "source": [
    "for i in df_temps.columns[1:]:\n",
    "    df_temps[i] = pd.to_numeric(df_temps[i], downcast = 'float')\n",
    "    \n",
    "print(\"Latest memory usage is: \", df_temps.memory_usage(deep=True).sum(), '\\n')\n",
    "\n",
    "df_temps.info()\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### 12) [P] How much did our memory footprint improve? (Show the total memory usage using deep=True). Report the total memory usage in MB, and report the percentage improvement."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Our memory footprint improved by  10188808\n",
      "\n",
      "Current memory usage is:  22561060\n",
      "\n",
      "Percent Improvement is:  145.16103410034813 %\n"
     ]
    }
   ],
   "source": [
    "print(\"Our memory footprint improved by \", original_memory - df_temps.memory_usage(deep=True).sum())\n",
    "print(\"\\nCurrent memory usage is: \",df_temps.memory_usage(deep=True).sum())\n",
    "print('\\nPercent Improvement is: ', 100 * original_memory/df_temps.memory_usage(deep=True).sum(), '%')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### 13) [M] There are four primary classes in pandas for working with dates and times? Consider the Scalar Class for each, and state what concept each is representing. (Again, if you think you are going to be working with data in your future, make some good notes for yourself here!)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The Date Times concept is representing a specific date and time with timezone support. The Time Deltas concept represents an absolute time duration. The Time Spans concept represents a span of time defined by a point in time and its associated frequency. The Date Offsets concept represents a relative time duration that represents calendar arithmetic.\n",
    "https://pandas.pydata.org/pandas-docs/stable/user_guide/timeseries.html"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### 14) [M] For each above, state the primary creation method used to create each type of data"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "For date time, the primary creation method is to_datetime or date_range. For time deltas, the primary creation method is to_timedelta or timedelta_range. For time spans, the primary creation method is period or period_range. For data offsets, the primary creation method is DataOffset.\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### 15) [P] Create a Timestamp object from the string \"07/04/19\", which is a date representing July 4, 2019. Store the object as d1 and show it."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2019-07-04 00:00:00\n"
     ]
    }
   ],
   "source": [
    "d1 = pd.Timestamp('2019/07/04')\n",
    "print(d1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### 16) [P] Using d1 and string formatting codes, print the string from d1: \"Today's date is Thursday, July 4, 2019\"."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Today's date is Thursday , July 4 , 2019 .\n"
     ]
    }
   ],
   "source": [
    "print(\"Today's date is\", d1.day_name(), ',', d1.month_name(), d1.day, ',', d1.year, \".\")\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### 17) [P] Create another Timestamp object representing Sept 7, 2019 at 3pm, called d2. Report it"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Timestamp('2019-09-07 15:00:00')"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d2 = pd.Timestamp('2019/09/07T03PM')\n",
    "d2"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### 18) [P] Subtract d2 – d1, and report the difference as the number of days and seconds between these two. Also report the difference as total seconds."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "65 days, 54000 seconds\n",
      "5670000.0 total seconds\n"
     ]
    }
   ],
   "source": [
    "print((d2-d1).days, \"days,\", (d2-d1).seconds, \"seconds\")\n",
    "print((d2-d1).total_seconds(), \"total seconds\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### 19) [P] Create a new Timestamp object from the string \"2019-07-01 08:30pm\", but localize the time stamp to represent the time in the US Eastern Time Zone. Store the result as d3 and output it."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2019-07-01 20:30:00-04:00\n"
     ]
    }
   ],
   "source": [
    "d3 = pd.Timestamp('2019-07-01T08:30PM', tz='US/Eastern')\n",
    "print(d3)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### 20) [P] Show time represented by d3, but converted to the US / Pacific Time Zone. The time reported should be three hours earlier than EST shown in the previous question."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Timestamp('2019-07-01 17:30:00-0700', tz='US/Pacific')"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d3.tz_convert('US/Pacific')\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### 21) [P] Create a Timestamp object representing right now, stored as ts_now. Show the result."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2021-02-23 15:08:04.704987\n"
     ]
    }
   ],
   "source": [
    "ts_now = pd.Timestamp.now()\n",
    "print(ts_now)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### 22) [P] Create a Timedelta object representing 1 hour, stored as td_hour. Show the result."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 days 01:00:00\n"
     ]
    }
   ],
   "source": [
    "td_hour = pd.Timedelta('1hour')\n",
    "print(td_hour)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### 23) [P] Demonstrate how you can do basic mathematical operations by adding 6 hours to ts_now using td_hour and basic math operations. (i.e. No loops or further calculations necessary!)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2021-02-23 21:08:04.704987\n"
     ]
    }
   ],
   "source": [
    "ts_now = ts_now + (td_hour * 6)\n",
    "print(ts_now)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### 24) [P] Create a DatetimeIndex object that represents every hour during the month of January, 2021. The first index should be midnight, January 1, 2021, and the last index should be January 31, 2021 at 11pm. Store the object as dr. (HINT – use the pd.date_range() method!)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "DatetimeIndex(['2021-01-01 00:00:00', '2021-01-01 01:00:00',\n",
      "               '2021-01-01 02:00:00', '2021-01-01 03:00:00',\n",
      "               '2021-01-01 04:00:00', '2021-01-01 05:00:00',\n",
      "               '2021-01-01 06:00:00', '2021-01-01 07:00:00',\n",
      "               '2021-01-01 08:00:00', '2021-01-01 09:00:00',\n",
      "               ...\n",
      "               '2021-01-31 14:00:00', '2021-01-31 15:00:00',\n",
      "               '2021-01-31 16:00:00', '2021-01-31 17:00:00',\n",
      "               '2021-01-31 18:00:00', '2021-01-31 19:00:00',\n",
      "               '2021-01-31 20:00:00', '2021-01-31 21:00:00',\n",
      "               '2021-01-31 22:00:00', '2021-01-31 23:00:00'],\n",
      "              dtype='datetime64[ns]', length=744, freq='60T')\n"
     ]
    }
   ],
   "source": [
    "dr = pd.date_range(start='1/1/2021', end='2/1/2021', freq=\"60min\", closed='left')\n",
    "print(dr)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### 25) [P] The first variable in our data is currently an object. But, notice the name and its units? It's a date/time in the GMT time zone! Convert the first column of data into an actual time stamp."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Date/Time (GMT)</th>\n",
       "      <th>Number of Observations (n/a)</th>\n",
       "      <th>Average Temp (F)</th>\n",
       "      <th>Max Temp (F)</th>\n",
       "      <th>Min Temp (F)</th>\n",
       "      <th>Average Dewpoint Temp (F)</th>\n",
       "      <th>1 Hour Precip (in)</th>\n",
       "      <th>Max Wind Gust (mph)</th>\n",
       "      <th>Average Relative Humidity (%)</th>\n",
       "      <th>Average Wind Speed (mph)</th>\n",
       "      <th>Average Station Pressure (mb)</th>\n",
       "      <th>Average Wind Direction (deg)</th>\n",
       "      <th>Max Wind Speed (mph)</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2000-01-01 00:00:00</td>\n",
       "      <td>1.0</td>\n",
       "      <td>26.100000</td>\n",
       "      <td>26.100000</td>\n",
       "      <td>26.1</td>\n",
       "      <td>14.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>20.700001</td>\n",
       "      <td>59.0</td>\n",
       "      <td>17.299999</td>\n",
       "      <td>1015.200012</td>\n",
       "      <td>280.000000</td>\n",
       "      <td>17.299999</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2000-01-01 01:00:00</td>\n",
       "      <td>1.0</td>\n",
       "      <td>26.100000</td>\n",
       "      <td>26.100000</td>\n",
       "      <td>26.1</td>\n",
       "      <td>14.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>59.0</td>\n",
       "      <td>16.100000</td>\n",
       "      <td>1015.900024</td>\n",
       "      <td>280.000000</td>\n",
       "      <td>16.100000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2000-01-01 02:00:00</td>\n",
       "      <td>1.0</td>\n",
       "      <td>26.100000</td>\n",
       "      <td>26.100000</td>\n",
       "      <td>26.1</td>\n",
       "      <td>15.1</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>62.0</td>\n",
       "      <td>15.000000</td>\n",
       "      <td>1016.299988</td>\n",
       "      <td>280.000000</td>\n",
       "      <td>15.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2000-01-01 03:00:00</td>\n",
       "      <td>1.0</td>\n",
       "      <td>26.100000</td>\n",
       "      <td>26.100000</td>\n",
       "      <td>26.1</td>\n",
       "      <td>12.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>54.0</td>\n",
       "      <td>16.100000</td>\n",
       "      <td>1016.599976</td>\n",
       "      <td>270.000000</td>\n",
       "      <td>16.100000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2000-01-01 04:00:00</td>\n",
       "      <td>1.0</td>\n",
       "      <td>26.100000</td>\n",
       "      <td>26.100000</td>\n",
       "      <td>26.1</td>\n",
       "      <td>14.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>59.0</td>\n",
       "      <td>12.700000</td>\n",
       "      <td>1017.299988</td>\n",
       "      <td>280.000000</td>\n",
       "      <td>12.700000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>181938</th>\n",
       "      <td>2020-12-31 19:00:00</td>\n",
       "      <td>3.0</td>\n",
       "      <td>37.599998</td>\n",
       "      <td>37.900002</td>\n",
       "      <td>37.0</td>\n",
       "      <td>27.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>65.0</td>\n",
       "      <td>7.700000</td>\n",
       "      <td>1023.400024</td>\n",
       "      <td>303.329987</td>\n",
       "      <td>10.400000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>181939</th>\n",
       "      <td>2020-12-31 20:00:00</td>\n",
       "      <td>1.0</td>\n",
       "      <td>37.000000</td>\n",
       "      <td>37.000000</td>\n",
       "      <td>37.0</td>\n",
       "      <td>26.1</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>64.0</td>\n",
       "      <td>5.800000</td>\n",
       "      <td>1024.699951</td>\n",
       "      <td>330.000000</td>\n",
       "      <td>5.800000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>181940</th>\n",
       "      <td>2020-12-31 21:00:00</td>\n",
       "      <td>1.0</td>\n",
       "      <td>36.000000</td>\n",
       "      <td>36.000000</td>\n",
       "      <td>36.0</td>\n",
       "      <td>25.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>64.0</td>\n",
       "      <td>6.900000</td>\n",
       "      <td>1025.400024</td>\n",
       "      <td>320.000000</td>\n",
       "      <td>6.900000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>181941</th>\n",
       "      <td>2020-12-31 22:00:00</td>\n",