Created
October 29, 2018 10:14
-
-
Save anielsen001/33731a356fe6dc2328b96a6781efc367 to your computer and use it in GitHub Desktop.
pandas datetime conversion options
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
try: | |
# python 3 | |
from io import StringIO | |
except: | |
# python 2 | |
from StringIO import StringIO | |
# some examples from | |
# https://stackoverflow.com/questions/17978092/combine-date-and-time-columns-using-python-pandas | |
data='''Date Time | |
01-06-2013 23:00:00 | |
02-06-2013 01:00:00 | |
02-06-2013 21:00:00 | |
02-06-2013 22:00:00 | |
02-06-2013 23:00:00 | |
03-06-2013 01:00:00 | |
03-06-2013 21:00:00 | |
03-06-2013 22:00:00 | |
03-06-2013 23:00:00 | |
04-06-2013 01:00:00 | |
''' | |
df = pd.read_csv(StringIO(data), delim_whitespace = True, parse_dates = [['Date','Time']]) | |
df['DateTime'] = pd.to_datetime(df.pop('Date')) + pd.to_timedelta(df.pop('Time')) | |
df["Date"] = pd.to_datetime(df["Date"]) | |
df["Time"] = pd.to_timedelta(df["Time"]) | |
df["DateTime"] = df["Date"] + df["Time"] | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment