Closed SophMC closed 8 years ago
Solution!
date_spec = {'date_time': [0,1,2]}
wind = pd.read_csv(datafile, sep=" ", names=column_names, parse_dates=date_spec, keep_date_col=True, index_col=False )
wind["hour"]=(wind["hour"]/100).astype(int)
wind['date_time'] = pd.to_datetime(wind.date_time) + wind.hour.astype('timedelta64[h]')
print wind.date_time[0:5]
0 1984-03-01 06:00:00 1 1984-03-01 12:00:00 2 1984-03-01 18:00:00 3 1984-03-02 06:00:00 4 1984-03-02 12:00:00 Name: date_time, dtype: datetime64[ns]
The light bulb moment came from discovering the line wind.hour.astype('timedelta64[h]')
@aaren I am reading in a txt file with the format
year month day hour ws
and want to create an extra column which takes the first four columns and makes a timestamp out of them and makes a new column. This is proving difficult for several reasons:
date_spec = {'date_time': [0,1,2]}
this works inwind
below.date_spec = {'date_time': [0,1,2,3]}
this doesn't. It creates a plain object fordate_time
, and we want a datetime object.wind = pd.read_csv('/home/sophie/projects/windspeed/data/61401BirMoghrein_allwinds.txt', sep=" ", names=column_names, parse_dates=date_spec, keep_date_col=True)
d = datetime(2013, 12, 22, 11, 30, 59); print d
works 2013-12-22 11:30:59years = [2012, 2013, 2014]
months=[5, 4, 3]
days=[6, 5, 4]
e = datetime(years, months, days); print e
doesn't workAs far as I can see you can only input for value at a time - it doesn't take lists.
conda update pandas
only took me up to 0.18.0 so they still don't work.https://pandas-docs.github.io/pandas-docs-travis/timeseries.html
pd.to_datetime(df[['year', 'month', 'day']])
doesn't work, substituting df=windThis other suggestion from the documentation (I copied exactly)
df = pd.DataFrame({'year': [2015, 2016], 'month': [2, 3],'day': [4, 5],'hour': [2, 3]})
pd.to_datetime(df)
doesn't workMaybe I can update to 0.18.1 some other way? Any ideas most welcome. I'll continue the fight tomorrow.