Creating date-time stamps
Contents
Creating date-time stamps#
Programming for Geoscientists Data Science and Machine Learning for Geoscientists
Let’s create columns with hours, minutes and seconds from utc_time column (contains strings) in format:
HH:MM:SS
by splitting the strings with a colon and creating float columns.
Let’s load New Zealand earthquake data:
import pandas as pd
nz_eqs = pd.read_csv("../../geosciences/data/nz_largest_eq_since_1970.csv")
nz_eqs.head(4)
year | month | day | utc_time | mag | lat | lon | depth_km | region | iris_id | timestamp | |
---|---|---|---|---|---|---|---|---|---|---|---|
0 | 2009 | 7 | 15 | 09:22:31 | 7.8 | -45.8339 | 166.6363 | 20.9 | OFF W. COAST OF S. ISLAND, N.Z. | 2871698 | 1247649751 |
1 | 2016 | 11 | 13 | 11:02:59 | 7.8 | -42.7245 | 173.0647 | 22.0 | SOUTH ISLAND, NEW ZEALAND | 5197722 | 1479034979 |
2 | 2003 | 8 | 21 | 12:12:47 | 7.2 | -45.0875 | 167.0892 | 6.8 | SOUTH ISLAND, NEW ZEALAND | 1628007 | 1061467967 |
3 | 2001 | 8 | 21 | 06:52:06 | 7.1 | -36.8010 | -179.7230 | 33.5 | EAST OF NORTH ISLAND, N.Z. | 1169374 | 998376726 |
nz_eqs["hour"] = nz_eqs["utc_time"].str.split(':').str.get(0).astype(float)
nz_eqs["minute"] = nz_eqs["utc_time"].str.split(':').str.get(1).astype(float)
nz_eqs["second"] = nz_eqs["utc_time"].str.split(':').str.get(2).astype(float)
With to_datetime() function, we can easily create date time stamp used by pandas:
nz_eqs["datetime"] = pd.to_datetime(nz_eqs[['year', 'month', 'day', 'hour', 'minute', 'second']])
nz_eqs.head(4)
year | month | day | utc_time | mag | lat | lon | depth_km | region | iris_id | timestamp | hour | minute | second | datetime | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | 2009 | 7 | 15 | 09:22:31 | 7.8 | -45.8339 | 166.6363 | 20.9 | OFF W. COAST OF S. ISLAND, N.Z. | 2871698 | 1247649751 | 9.0 | 22.0 | 31.0 | 2009-07-15 09:22:31 |
1 | 2016 | 11 | 13 | 11:02:59 | 7.8 | -42.7245 | 173.0647 | 22.0 | SOUTH ISLAND, NEW ZEALAND | 5197722 | 1479034979 | 11.0 | 2.0 | 59.0 | 2016-11-13 11:02:59 |
2 | 2003 | 8 | 21 | 12:12:47 | 7.2 | -45.0875 | 167.0892 | 6.8 | SOUTH ISLAND, NEW ZEALAND | 1628007 | 1061467967 | 12.0 | 12.0 | 47.0 | 2003-08-21 12:12:47 |
3 | 2001 | 8 | 21 | 06:52:06 | 7.1 | -36.8010 | -179.7230 | 33.5 | EAST OF NORTH ISLAND, N.Z. | 1169374 | 998376726 | 6.0 | 52.0 | 6.0 | 2001-08-21 06:52:06 |
The datetime column is not yet recognised as index so we can simply use:
nz_eqs = nz_eqs.set_index('datetime')
This timestamp will be useful for plotting data.
References#
The notebook was compiled based on: