Skip to content

Instantly share code, notes, and snippets.

@tommylees112
Last active August 23, 2021 14:44
Show Gist options
  • Save tommylees112/28e32b0153de004ab8db19170ae02a94 to your computer and use it in GitHub Desktop.
Save tommylees112/28e32b0153de004ab8db19170ae02a94 to your computer and use it in GitHub Desktop.
Using the year (int) and fraction of year (float) to create datetime objects [NOTE: assumes that data is of daily frequency!]
from typing import Tuple, Any, Union, List
import pandas as pd
import datetime as dt
from pathlib import Path
import numpy as np
def find_nearest(array: Union[np.ndarray, List[float]], value: float) -> Tuple[Any, int]:
# https://stackoverflow.com/a/2566508/9940782
array = np.asarray(array)
idx = (np.abs(array - value)).argmin()
return array[idx], idx
if __name__ == "__main__":
data_dir = Path("home/tommylees/Downloads/")
df = pd.read_csv(data_dir / "slr_sla_nrs_free_txj1j2_90.csv", skiprows=5)
# convert columns into years and frac of years
years = [int(row[0]) for row in df["year"].astype(str).str.split(".")]
frac_of_years = [float(f"0.{row[1]}") for row in df["year"].astype(str).str.split(".")]
# calculate each day-of-year encoded as a fraction, to be used as a lookup list
#  https://stackoverflow.com/a/56193202/9940782
reference_fractions = [dt.timedelta(days=i) / dt.timedelta(days=365) for i in range(365)]
# get the nearest match in the list of reference_fractions
# add 1 because of zero based indexing
doys = [find_nearest(reference_fractions, value=f)[-1] + 1 for f in frac_of_years]
# convert to datetime
datetimes = [dt.datetime(year=year, month=1, day=1) + dt.timedelta(days=int(doy)) for year, doy in zip(years, doys)]
# append back as new columns
df["time"] = datetimes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment