python-datetime

Convert dataframe column to datetime only if length of string is not zero

给你一囗甜甜゛ 提交于 2021-01-29 07:02:17
问题 I'd like to convert a dataframe column which has a date string. But in some cases, the date string might be empty due to certain conditions. So I just want all the other rows in that column to be converted to datetime format except the rows in that particular column which might be blank. Is it possible? What I've tried so far: Option1: df['etime'] = pd.to_datetime(df['etime'],errors='ignore').dt.strftime('%Y-%m-%d %H:%M') Option 2: for ind in df.index: if (df['etime'].str.len()[ind] == 0) :

Get the time spent since midnight in dataframe

最后都变了- 提交于 2021-01-28 09:59:55
问题 I have a dataframe which has a column of type Timestamp. I want to find the time elapsed (in seconds) since midnight as a new column. How to do it in a simple way ? Eg : Input : samples['time'] 2018-10-01 00:00:01.000000000 2018-10-01 00:00:12.000000000 type(samples['time'].iloc[0]) <class 'pandas._libs.tslib.Timestamp'> Output : samples['time_elapsed'] 1 12 回答1: Note that the date part in each row may be other (not from one and the same day), so you can not take any "base date" ( midnight )

Get the time spent since midnight in dataframe

心已入冬 提交于 2021-01-28 09:52:45
问题 I have a dataframe which has a column of type Timestamp. I want to find the time elapsed (in seconds) since midnight as a new column. How to do it in a simple way ? Eg : Input : samples['time'] 2018-10-01 00:00:01.000000000 2018-10-01 00:00:12.000000000 type(samples['time'].iloc[0]) <class 'pandas._libs.tslib.Timestamp'> Output : samples['time_elapsed'] 1 12 回答1: Note that the date part in each row may be other (not from one and the same day), so you can not take any "base date" ( midnight )

pandas count values for last 7 days from each date

此生再无相见时 提交于 2021-01-21 08:44:07
问题 There are two Dataframes. First is like this: print df1 id date month is_buy 0 17 2015-01-16 2015-01 1 1 17 2015-01-26 2015-01 1 2 17 2015-01-27 2015-01 1 3 17 2015-02-11 2015-02 1 4 17 2015-03-14 2015-03 1 5 18 2015-01-28 2015-01 1 6 18 2015-02-12 2015-02 1 7 18 2015-02-25 2015-02 1 8 18 2015-03-04 2015-03 1 In second data frame there are some aggregated data by month from the first one: df2 = df1[df1['is_buy'] == 1].groupby(['id', 'month']).agg({'is_buy': np.sum}) print df2 id month buys 0

how can I resample pandas dataframe by day on period time?

爷,独闯天下 提交于 2021-01-07 04:00:13
问题 i have a dataframe like this: df.head() Out[2]: price sale_date 0 477,000,000 1396/10/30 1 608,700,000 1396/10/30 2 580,000,000 1396/10/03 3 350,000,000 1396/10/03 4 328,000,000 1396/03/18 that it has out of bounds datetime so then i follow below to make them as period time df['sale_date']=df['sale_date'].str.replace('/','').astype(int) def conv(x): return pd.Period(year=x // 10000, month=x // 100 % 100, day=x % 100, freq='D') df['sale_date'] = df['sale_date'].str.replace('/','').astype(int)

Get the number of week days with date between two dates in python

萝らか妹 提交于 2021-01-05 12:44:31
问题 As I have searched in website to get the number of weekdays in python, but I need the date of the weekdays too. My input will be: start date = 01-03-2019 end_date = 15-03-2019 days = monday , tuesday Expected Output : which I need is to print the number of mondays and tuesdays along with the date. 回答1: Try this : start_date = '01-03-2019' # Considering 03 is the month, 01 is the day end_date = '15-03-2019' # Considering 03 is the month, 15 is the day start_date = [int(i) for i in start_date

Get the number of week days with date between two dates in python

╄→гoц情女王★ 提交于 2021-01-05 12:43:06
问题 As I have searched in website to get the number of weekdays in python, but I need the date of the weekdays too. My input will be: start date = 01-03-2019 end_date = 15-03-2019 days = monday , tuesday Expected Output : which I need is to print the number of mondays and tuesdays along with the date. 回答1: Try this : start_date = '01-03-2019' # Considering 03 is the month, 01 is the day end_date = '15-03-2019' # Considering 03 is the month, 15 is the day start_date = [int(i) for i in start_date

Get the number of week days with date between two dates in python

跟風遠走 提交于 2021-01-05 12:42:52
问题 As I have searched in website to get the number of weekdays in python, but I need the date of the weekdays too. My input will be: start date = 01-03-2019 end_date = 15-03-2019 days = monday , tuesday Expected Output : which I need is to print the number of mondays and tuesdays along with the date. 回答1: Try this : start_date = '01-03-2019' # Considering 03 is the month, 01 is the day end_date = '15-03-2019' # Considering 03 is the month, 15 is the day start_date = [int(i) for i in start_date

how to send datetime dataframe to django template and plot it in js using plotly

痞子三分冷 提交于 2021-01-05 07:09:30
问题 I have a data-frame and I want to send it in my Django template. dummy code in views.py : def graphs(request): df_new = pd.read_excel("/home/cms/cms/static/Sensorik.xlsx") times = df_new.loc[:, df_new.columns[0]].to_json(orient='records') # columns[0] contains datetime values data_color = df_georgian.loc[:, df_georgian.columns[2]] color = data_color.to_json(orient='records') context = { 'times': times, 'data_color': color, ... } return render(request, 'graphs/graphs.html', context) In my

how to send datetime dataframe to django template and plot it in js using plotly

瘦欲@ 提交于 2021-01-05 07:07:28
问题 I have a data-frame and I want to send it in my Django template. dummy code in views.py : def graphs(request): df_new = pd.read_excel("/home/cms/cms/static/Sensorik.xlsx") times = df_new.loc[:, df_new.columns[0]].to_json(orient='records') # columns[0] contains datetime values data_color = df_georgian.loc[:, df_georgian.columns[2]] color = data_color.to_json(orient='records') context = { 'times': times, 'data_color': color, ... } return render(request, 'graphs/graphs.html', context) In my