Can only use dt accessor with datetimelike values

Video Can only use dt accessor with datetimelike values

On my django app ,I gived this error :

AttributeError at / Can only use .dt accessor with datetimelike values line 28, in home results,output_df,new =results1(file_directory,file_directory2) line 111, in results1 new = output_df.groupby([output_df[‘Date and Time’], ‘PCR POS/Neg’]).size().unstack(fill_value=0)

Who can explain me what i have to change ?

from django.shortcuts import render from import FileSystemStorage import pandas as pd import datetime from datetime import datetime as td import os from collections import defaultdict def home(request): if request.method == ‘POST’: uploaded_file = request.FILES[‘document’] uploaded_file2 = request.FILES[‘document2’] if‘.xls’): savefile = FileSystemStorage() name =, uploaded_file) name2 =, uploaded_file2) d = os.getcwd() file_directory = d+’\media\’+name file_directory2 = d+’\media\’+name2 results,output_df,new =results1(file_directory,file_directory2) return render(request,”results.html”,{“results”:results,”output_df”:output_df,”new”:new}) return render(request, “index.html”) def readfile(uploaded_file): data = pd.read_excel(uploaded_file, index_col=None) return data def results1(file1,file2): results_list = defaultdict(list) names_loc = file2 listing_file = pd.read_excel(file1, index_col=None) with open(names_loc, “r”) as fp: for line in fp.readlines(): line = line.rstrip(“\n”) full_name = line.split(‘,’) sample_name = full_name[0].split(‘_mean’) try: if len(sample_name[0].split(‘SCO_’)) > 1: sample_id = int(sample_name[0].split(‘SCO_’)[1]) else: sample_id = int(sample_name[0].split(‘SCO’)[1]) except: sample_id = sample_name[0] try: if listing_file[‘Test ID’].isin([sample_id]).any(): line_data = listing_file.loc[listing_file[‘Test ID’].isin([sample_id])] vector_name = line d_t = full_name[1].split(‘us_’)[1].split(‘_’) date_time = td(int(d_t[0]), int(d_t[1]), int(d_t[2]), int(d_t[3]), int(d_t[4]), int(d_t[5])) date_index = list(line_data[‘Collecting Date from the subject’].iteritems()) for x in date_index: if type(x[1]) is str(): date_time_obj = td.strptime(x[1], ‘%Y.%m.%d. %H:%M’) elif type(x[1]) is pd.Timestamp: date_time_obj = x[1] elif type(x[1]) is datetime.datetime: date_time_obj = x[1] frame_time = str(date_time – date_time_obj) if date_time – date_time_obj > datetime.timedelta(hours=48): results_list[“List 1”].append(sample_id) test_id = sample_id pcr_index = list(line_data[‘PCR Pos/Neg’].iteritems()) if len(pcr_index) > 1: results_list[“List 2”].append(sample_id) for x in pcr_index: pcr_ans = x[1].strip() values_to_add = {‘Vector Name’: vector_name, ‘Date and Time’: date_time, ‘Test ID’: test_id, ‘PCR POS/Neg’: pcr_ans, ‘Time Frame’: frame_time } row_to_add = pd.Series(values_to_add) output_df = output_df.append(row_to_add, ignore_index=True) else: results_list[“List 3 “].append(sample_name[0]) except: print(‘not good: {}’.format(sample_id)) new = output_df.groupby([output_df[‘Date and Time’], ‘PCR POS/Neg’]).size().unstack(fill_value=0) new.sort_values(by=[‘Date and Time’], ascending=True) new[‘Total per date’] = output_df.groupby([output_df[‘Date and Time’]])[‘PCR POS/Neg’].count() new.loc[‘Total’, :] = new.sum(axis=0) return dict(results_list), output_df.to_html(), new.to_html() Back to Top

Related Posts