timedelta

Adding one month to datetime64 with timedelta

自古美人都是妖i 提交于 2020-05-14 09:04:23
问题 When I try this: >>> a = numpy.datetime64('1995-12-31') >>> b = a + pandas.Timedelta(1, unit='M') >>> print(b) I expect to see 1996-01-31 but instead I get 1996-01-30 10:29:06. Any idea why? Many thanks. 回答1: A time delta of one month is the length of a year divided by 12. You need to examine your date and add the appropriate quantity of days. Alternately, increment the month number (rolling over to the next year, if needed), and leave the day number unchanged. 回答2: You can replace the day

formatting timedelta64 when using pandas.to_excel

五迷三道 提交于 2020-05-13 04:40:27
问题 I am writing to an excel file using an ExcelWriter : writer = pd.ExcelWriter(fn,datetime_format=' d hh:mm:ss') df.to_excel(writer,sheet_name='FOO') The writing operation is successful and opening the corresponding excel file I see datetimes nicely formatted as required. However, another column of the dataframe with dtype timedelta64[ns] is automatically converted to a numerical value, so in Python I see 0 days 00:23:33.499998 while in excel: 0.016359954 which is likely the same duration

formatting timedelta64 when using pandas.to_excel

a 夏天 提交于 2020-05-13 04:40:24
问题 I am writing to an excel file using an ExcelWriter : writer = pd.ExcelWriter(fn,datetime_format=' d hh:mm:ss') df.to_excel(writer,sheet_name='FOO') The writing operation is successful and opening the corresponding excel file I see datetimes nicely formatted as required. However, another column of the dataframe with dtype timedelta64[ns] is automatically converted to a numerical value, so in Python I see 0 days 00:23:33.499998 while in excel: 0.016359954 which is likely the same duration

Generate list of datetimes with hourly timedelta

风格不统一 提交于 2020-03-24 03:59:12
问题 I am using this code to generate a range of hourly datetime 's: from datetime import datetime, timedelta def daterange(start_date, end_date): # timedelta only has days and seconds attributes for n in range(int ((end_date - start_date).seconds/3600 + 1)): yield start_date + timedelta(n) start_date = datetime(2013, 1, 1, 14, 00) end_date = datetime(2015, 6, 2, 5, 00) for single_date in daterange(start_date, end_date): print single_date.strftime("%Y-%m-%d %H:%M") But I am getting an unexpected

Generate list of datetimes with hourly timedelta

拟墨画扇 提交于 2020-03-24 03:56:08
问题 I am using this code to generate a range of hourly datetime 's: from datetime import datetime, timedelta def daterange(start_date, end_date): # timedelta only has days and seconds attributes for n in range(int ((end_date - start_date).seconds/3600 + 1)): yield start_date + timedelta(n) start_date = datetime(2013, 1, 1, 14, 00) end_date = datetime(2015, 6, 2, 5, 00) for single_date in daterange(start_date, end_date): print single_date.strftime("%Y-%m-%d %H:%M") But I am getting an unexpected

Pandas: increment datetime

冷暖自知 提交于 2020-02-01 04:39:06
问题 I need to do some actions with date in df column buys['date_min'] = (buys['date'] - MonthDelta(1)) buys['date_min'] = (buys['date'] + timedelta(days=5)) But it return TypeError: incompatible type [object] for a datetime/timedelta operation How can I do it to column? 回答1: I think you need first convert column date to_datetime, because type od values in column date is string : buys['date_min'] = (pd.to_datetime(buys['date']) - MonthDelta(1)) buys['date_min'] = (pd.to_datetime(buys['date']) +

from string to timedelta in pandas

白昼怎懂夜的黑 提交于 2020-01-24 22:20:33
问题 I have a dataframe where the timestamp is in the format HHHHH:MM timestamp = pd.Series(['34:23','125:26','15234:52'], index=index) I would like to convert it to a timedelta. For now I manage to do that on a single string str[:-3] str[-2:] timedelta(hours=int(str[:-3]),minutes=int(str[-2:])) I would like to apply it to the whole serie, if possible in a cleaner way. Does it exist? Thanks, 回答1: You can use column-wise Pandas methods: s = pd.Series(['34:23','125:26','15234:52']) v = s.str.split('

How to avoid time being generated after subtracting timedelta

余生颓废 提交于 2020-01-24 20:15:50
问题 I have a dataframe which look like this as below Year Birthday OnsetDate 5 2018/1/1 5 2018/2/2 now I use the OnsetDate column subtract with the Day column df['Birthday'] = df['OnsetDate'] - pd.to_timedelta(df['Day'], unit='Y') but the outcome of the Birthday column is mixing with time just like below Birthday 2013/12/31 18:54:00 2013/1/30 18:54:00 the outcome is just a dummy data, what I focused on this is that the time will cause inaccurate of date after the operation. What is the solution

how set column as date index?

 ̄綄美尐妖づ 提交于 2020-01-22 17:38:05
问题 My data sets looks like: Date Value 1/1/1988 0.62 1/2/1988 0.64 1/3/1988 0.65 1/4/1988 0.66 1/5/1988 0.67 1/6/1988 0.66 1/7/1988 0.64 1/8/1988 0.66 1/9/1988 0.65 1/10/1988 0.65 1/11/1988 0.64 1/12/1988 0.66 1/13/1988 0.67 1/14/1988 0.66 1/15/1988 0.65 1/16/1988 0.64 1/17/1988 0.62 1/18/1988 0.64 1/19/1988 0.62 1/20/1988 0.62 1/21/1988 0.64 1/22/1988 0.62 1/23/1988 0.60 I used this code to read this data df.set_index(df['Date'], drop=False, append=False, inplace=False, verify_integrity=False)

how set column as date index?

蓝咒 提交于 2020-01-22 17:37:01
问题 My data sets looks like: Date Value 1/1/1988 0.62 1/2/1988 0.64 1/3/1988 0.65 1/4/1988 0.66 1/5/1988 0.67 1/6/1988 0.66 1/7/1988 0.64 1/8/1988 0.66 1/9/1988 0.65 1/10/1988 0.65 1/11/1988 0.64 1/12/1988 0.66 1/13/1988 0.67 1/14/1988 0.66 1/15/1988 0.65 1/16/1988 0.64 1/17/1988 0.62 1/18/1988 0.64 1/19/1988 0.62 1/20/1988 0.62 1/21/1988 0.64 1/22/1988 0.62 1/23/1988 0.60 I used this code to read this data df.set_index(df['Date'], drop=False, append=False, inplace=False, verify_integrity=False)