Number of samples, -5, must be non-negative using panda python - python-3.x

I have a csv file with date and time. I want to give specific timeinterval (60min) in between time range (start time and end time). I wrote a code with a date. But it gives me an error Number of samples, -5, must be non-negative. Then I checked with separate csv file with less data. Then I found that I have time like 9:53 , 10:20 ,11:42 .... Then when I'm dividing to find num_periods then its give me an error.
example
take date range like
2018 /8/6 start time is 6:00
2018/8/6 end time is 23:52
then it between I have time like 7:00, 8:52,10:42 so on.
after that in next day I have a time period like this.
So when I tried to find a num_periods then it give me this error.
I want to specify time in between this time_range
(start_time+time_interval(3600 in seconds (60min)) in between time_range)
Can anyone give me solution for this?
my code is,
time_interval = 3600
date_array = []
date_array.append(pd.to_datetime(data['date'][0]).date())
start_time = []
end_time = []
temp_date = pd.to_datetime(data['date'][0]).date()
start_time.append(pd.to_datetime(data['time'][0], format='%H:%M:%S').time())
for i in range(len(data['date'])):
cur_date = pd.to_datetime(data['date'][i]).date()
if( cur_date > temp_date):
end_time.append(pd.to_datetime(data['time'][i-1], format='%H:%M:%S').time())
start_time.append(pd.to_datetime(data['time'][i], format='%H:%M:%S').time())
date_array.append(cur_date)
temp_date = cur_date
end_time.append(pd.to_datetime(data['time'][len(data['date'])-1], format='%H:%M:%S').time())
datetime_array = []
for i in range(len(date_array)):
s_time = datetime.datetime.combine(date_array[i],start_time[i])
e_time = datetime.datetime.combine(date_array[i], end_time[i])
timediff = (e_time - s_time)
num_periods = int(timediff.total_seconds()/time_interval) +1
time_list = pd.date_range(start=s_time, end = e_time, periods=num_periods ).to_pydatetime()
datetime_array.extend(time_list)
error:
subset of my csv file

It looks like num_periods is negative:
num_periods = int(timediff.total_seconds()/time_interval) + 1
the easiest solution is to take the abs value instead:
num_periods = abs(int(timediff.total_seconds()/time_interval)) + 1
Note: that date_range supports ranges in reverse order (where start > end).

Related

Reading excel values as seconds and convert to minutes

I am currently writing a lager script to ease my life.
Right now I am reading raw values from cells from an excel.
So far so good.
These numbers need to be interpreted as seconds and then converted into minutes.
I tried my best with datetime but no luck.
Any suggestions?
elif auswahl == '2':
print("Some user friendly-text:")
excel_download = openpyxl.load_workbook(r'/Path/to/excel.xlsx')
sheet = excel_download.active
Grund_1 = sheet['B2'].value
Grund_2 = sheet['B3'].value
Grund_3 = sheet['B4'].value
Grund_4 = sheet['B5'].value
Grund_5 = sheet['B6'].value
Grund_6 = sheet['B7'].value
Zeit_in_Sekunden_1 = sheet['C2'].value
Zeit_in_Sekunden_2 = sheet['C3'].value
Zeit_in_Sekunden_3 = sheet['C4'].value
Zeit_in_Sekunden_4 = sheet['C5'].value
Zeit_in_Sekunden_5 = sheet['C6'].value
Zeit_in_Sekunden_6 = sheet['C7'].value
print("Du warst heute für", Zeit_in_Sekunden_1, Grund_1, "!")
break
My idea:
raw_seconds_from_C2 = sheet['C2'].value
Then somehow convert to minutes from raw_seconds_from_C2
I am really out of ideas as I then need to put the converted minutes into a print().
Divide the value by 60 to obtain minutes from seconds:
c2_minutes = sheet['C2'].value / 60
Thanks to #Alonso's comment on the question.

How to remove milliseconds in datetime python?

I want to print the amount of days, hours, minutes and seconds until christmas day but when it prints, it also gives me the millisecond. I tried doing print(remain_until_xmas.strip(.) but that didn't work. Here is the code
import datetime as dt
xmas = dt.datetime(2020, 12, 25)
now = dt.datetime.now()
until_xmas = xmas - now
print(until_xmas)
To get the time live, you can use this code:
import time
time = str(time.strftime("%M")) + ":" + str(time.strftime("%S"))
print(time)
This will give you the current time in Minutes and Seconds, and if you put it in a loop, it will keep updating itself.
If you want the day and month, you can use
print(time.strftime("%A, %B %e")) -
With this code, you can then subtract the Xmas date from the current date, and retrieve what you want.
This would be your final code:
import time
month = time.strftime("%m")
day = time.strftime("%d")
hour = time.strftime("%H")
minute = time.strftime("%M")
second = time.strftime("%S")
chrmonth = 12
chrday = 23
chrhour = 12
chrminute = 60
chrsecond = 60
print("The time until christmas is, ", int(chrmonth) - int(month), "months", int(chrday) - int(day), "days", int(chrhour) - int(hour), "hours", int(chrminute) - int(minute), "minutes", int(chrsecond) - int(second), "seconds")
Hopefully this helps!
you can use .strftime()
print(until_xmas.strftime("%m/%d/%Y, %H:%M:%S"))
see more here about strftime.

Date Time difference between hour of the day and time now

Time = datetime.datetime.now().time()
I wanted to find the difference in seconds between the Time above^ and the hour of the day:
for example if the Time = 15:07:25.097519, so the hour of the day is 15:00:00, i want to store 445.097519‬ seconds to a variable.
How can I do that?
I am an amateur at this please help!!
import datetime
now = datetime.datetime.now()
hour = now.replace(minute=0, second=0, microsecond=0)
seconds = (now - hour).seconds + (now - hour).microseconds / 1000000
Here is one way to do it. Extract the hour from current time, and initialize a new datetime.time object with hour as input. Then you convert both of the timestamps to datetime and then do the subtraction. Code below does that
Time = datetime.datetime.now().time()
hour = str(Time).split(":")[0]
currentHourTime = datetime.datetime(2019,10,31,int(hour),0,0,0).time()
dateTimeCurr = datetime.datetime.combine(datetime.date.today(), Time)
dateTimeCurrHour = datetime.datetime.combine(datetime.date.today(), currentHourTime)
dateTimeDifference = dateTimeCurr - dateTimeCurrHour
dateTimeDifferenceInSeconds = dateTimeDifference.total_seconds()
print(dateTimeDifferenceInSeconds)

Setting variable to compare file modification between multiple dates

I'm trying to compare two text files that are date specific, but I'm stumped. I created a test folder that has three text files in it with modified dates between one and 35 days old.
I.E: red.txt is 35 days old, blue.txt is one day old, and green.txt is 15 days old.
For my two compared files, the first file must be between a range of 13-15 days and the second one day old or less. So for this example, 'green.txt' will become 'file1' and 'blue.txt' will become 'file2' and then be compared with difflib, but I'm having trouble with the syntax, or maybe even the logic. I am using datetime with timedelta to try to get this working, but my results will always store the oldest modified file that is past 15 days for 'file1'. Here's my code:
import os, glob, sys, difflib, datetime as d
p_path = 'C:/test/Text_file_compare_test/'
f_list = glob.glob(os.path.join(p_path, '*.txt'))
file1 = ''
file2 = ''
min_days_ago = d.datetime.now() - d.timedelta(days=1)
max_days_ago = d.datetime.now() - d.timedelta(days=13 <= 15)
for file in f_list:
filetime = d.datetime.fromtimestamp(os.path.getmtime(file))
if filetime < max_days_ago:
file1 = file
if filetime > min_days_ago:
file2 = file
with open(file1) as f1, open(file2) as f2:
d = difflib.Differ()
result = list(d.compare(f1.readlines(), f2.readlines()))
sys.stdout.writelines(result)
I'm certain there is something wrong with code:
max_days_ago = d.datetime.now() - d.timedelta(days=13 <= 15)
Maybe I'm just not seeing something in the datetime module that's obvious. Can someone shed some light for me? Also, this is on Windows 10 Python 3.7.2. Thanks in advance!
As per my comment, your d.timedelta(days=13 <= 15) isn't quite right as you are assigning days to a boolean value of true, which will be equivalent to d.timedelta(days=1). You need to store 3 separate time points and do your 13-15 day comparison against two different dates. The code below demonstrates what you are looking for I believe:
import datetime as d
files = {
'red': d.datetime.now() - d.timedelta(days=35),
'blue': d.datetime.now() - d.timedelta(days=0, hours=12),
'green': d.datetime.now() - d.timedelta(days=14),
}
days_ago_1 = d.datetime.now() - d.timedelta(days=1)
days_ago_13 = d.datetime.now() - d.timedelta(days=13)
days_ago_15 = d.datetime.now() - d.timedelta(days=15)
file1 = None
file2 = None
for file, filetime in files.items():
if days_ago_13 >= filetime >= days_ago_15:
file1 = file
elif filetime > days_ago_1:
file2 = file
# need to break out of the loop when we are finished
if file1 and file2:
break
print(file1, file2)
prints green blue

Insert rows into Excel with MATLAB

The data in my Excel files is supposed to be contentious (index in the first column). But some data is missing in the file. For example, # 5, and 6 are missing between $ 4 and 7. My purpose are (1) identify the file with missing data and (2) if data is missing insert rows to make it continuous. Can anyone tell me how to add in rows in the existing data? Using xlswrite I can only add in rows at the end of the file or replace some rows.
EDIT 1:
I have another set of file in which the index is not so direct. The first 3 columns are described below (as shown in the Excel file):
Column 1:Year: 2003 (read as number in matlab)
Column 2:Date: 1-Sep (read as text in matlab)
Column 3:Time: 1:00 (1:00 read as number 0.04167 and 2:00 read as 0.0833, not sure how it works)
Then the way to tell if it is continuous will be quite complicate since there will be different years, months, and days. Could you give some hint on this?
Basically you need to read the entire data, preferably in raw(cell) format, add the missing rows(with respect to the indices) and write back.
Based on your question, this code might work -
% NOTE: We are assuming that the indexing starts with 1
% Read data from input excel file with missing indices
[num,txt,raw] = xlsread('input.xls');
% Error-checking
if (size(num,1)-num(end,1)~=0)
disp('At least one index is missing!');
end
% Expand data such that all indices are covered.
data1=NaN(num(end,1),size(raw,2));
data1(:,1) = 1:num(end,1);
data1=num2cell(data1);
k1=1;
for k = 1:num(end,1)
if(num(k1,1)==k)
data1(k,:)= raw(k1,:);
k1 = k1+1;
end
end
% Write data
xlswrite('output.xls',data1);
EDIT 1:
In view of your new requirements, additional code is added next.
Please note few things about this code -
The code adds data for every year and not from a specific month, date and time to another specific month, date and time. If you wish to achieve that, please edit the associated
function - 'create_comp_sheet'.
It saves an intermediate file named - 'proper_base_data.xls', which maybe deleted at the end of the code.
%% MAIN CODE - CODE1.M
INPUT_FILENAME = 'input.xls'; % Excel file that has some missing year,date and time info
OUTPUT_FILENAME = 'output.xls'; % Excel file that has data from the input file along with all the missing year,date and time info
%% Base data
start_year=2003;
end_year=2005;
proper_base_data = create_comp_sheet(start_year,end_year);
xlswrite('proper_base_data.xls',proper_base_data);
[num,txt,raw] = xlsread('proper_base_data.xls');
base_data=cell(size(num,1),1);
for row_ID = 1:size(num,1)
base_data(row_ID) = {strcat(num2str(cell2mat(raw(row_ID,1))),'-', cell2mat(raw(row_ID,2)),'-',num2str(round(24*cell2mat(raw(row_ID,3)))))};
end
%% Input data
[num,txt,raw] = xlsread(INPUT_FILENAME);
input_data=cell(size(num,1),1);
for row_ID = 1:size(num,1)
input_data(row_ID) = {strcat(num2str(cell2mat(raw(row_ID,1))),'-', cell2mat(raw(row_ID,2)),'-',num2str(round(24*cell2mat(raw(row_ID,3)))))};
end
%% Setup final data
final_data = num2cell(NaN(size(proper_base_data,1),size(raw,2)));
final_data(:,1:3) = proper_base_data;
for k1=1:size(input_data,1)
for k2=1:size(base_data,1)
if strcmp(cell2mat(base_data(k2)),cell2mat(input_data(k1)))
final_data(k2,4:end) = raw(k1,4:end);
end
end
end
%% Write final data to excel
xlswrite(OUTPUT_FILENAME,final_data);
Associated function -
function data1 = create_comp_sheet(start_year,end_year)
months_string = {'Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'};
date_count = [31 28 31 30 31 30 31 31 30 31 30 31];
num_hours = 24;
data1=[];
for year_ID = start_year:end_year
for month_ID = 1:numel(months_string)
days_per_month = date_count(month_ID);
if rem(year_ID,4)==0 && month_ID ==2
days_per_month = days_per_month+1;
end
for date_ID = 1:days_per_month
year = repmat({num2str(year_ID)},[num_hours 1]);
date = repmat({strcat(num2str(date_ID),'-',char(months_string(month_ID)))},[num_hours 1]);
time=cell(num_hours,1);
for k = 1:num_hours
time(k) = {strcat(num2str(k),':00')};
end
data1 = [data1 ; [year date time]];
end
end
end
return;
Hope this saves all your troubles!

Resources