How To Reset Time In DateTime Column Pandas Dataframe - pandas

I'm trying to convert date time field to date only, but when I do that the dtype changed to object, so I'm thinking to reset all time in all rows to be zeros
from
Price Volume
2011-08-14 14:14:40 10.4 0.779
2011-08-14 15:15:17 10.4 0.101
2011-08-14 15:15:17 10.4 0.316
2011-08-14 16:45:09 10.5 0.150
2011-08-14 16:45:09 10.5 1.800
to
Price Volume
2011-08-14 00:00:00 10.4 0.779
2011-08-14 00:00:00 10.4 0.101
2011-08-14 00:00:00 10.4 0.316
2011-08-14 00:00:00 10.5 0.150
2011-08-14 00:00:00 10.5 1.800
how can I do that ?
many thanks

Related

pandas (multi) index wrong need to change it

I have a DataFrame multiData that looks like this:
print(multiData)
Date Open High Low Close Adj Close Volume
Ticker Date
AAPL 0 2010-01-04 7.62 7.66 7.59 7.64 6.51 493729600
1 2010-01-05 7.66 7.70 7.62 7.66 6.52 601904800
2 2010-01-06 7.66 7.69 7.53 7.53 6.41 552160000
3 2010-01-07 7.56 7.57 7.47 7.52 6.40 477131200
4 2010-01-08 7.51 7.57 7.47 7.57 6.44 447610800
... ... ... ... ... ... ... ...
META 2668 2022-12-23 116.03 118.18 115.54 118.04 118.04 17796600
2669 2022-12-27 117.93 118.60 116.05 116.88 116.88 21392300
2670 2022-12-28 116.25 118.15 115.51 115.62 115.62 19612500
2671 2022-12-29 116.40 121.03 115.77 120.26 120.26 22366200
2672 2022-12-30 118.16 120.42 117.74 120.34 120.34 19492100
I need to get rid of "Date 0, 1, 2, ..." column and make the actual "Date" column part of the (multi) index
How do I do this?
Use df.droplevel to delete level 1 and chain df.set_index to add column Date to the index by setting the append parameter to True.
df = df.droplevel(1).set_index('Date', append=True)
df
Open High Low Close Adj Close Volume
Ticker Date
AAPL 2010-01-04 7.62 7.66 7.59 7.64 6.51 493729600
2010-01-05 7.66 7.70 7.62 7.66 6.52 601904800

missing data in pandas web.DataReader yahoo

I am collecting data from yahoo, it works fine for more than 3 months. Then in last week I noticed that datas from 22.4.2021 is missing till today.
When I print the stockData, here is the last records :
...
...
2021-04-20 7.52 7.07 7.35 7.13 4119197.0 7.13
2021-04-21 7.36 7.13 7.16 7.25 3110870.0 7.25
2021-04-22 7.73 7.10 7.22 7.59 13178439.0 7.59
2021-05-06 10.08 9.48 9.52 9.93 2753885.0 9.93
As you see, datas are missing. It's because of yahoo or panda function, Could you help me to fix it ?.
Alper

subtraction in SQL giving incorrect value

I have a table that contains Id, Date and a float value as below:
ID startDt Days
1328 2015-04-01 00:00:00.000 15
2444 2015-04-03 00:00:00.000 5.7
1658 2015-05-08 00:00:00.000 6
1329 2015-05-12 00:00:00.000 28.5
1849 2015-06-23 00:00:00.000 28.5
1581 2015-06-30 00:00:00.000 25.5
3535 2015-07-03 00:00:00.000 3
3536 2015-08-13 00:00:00.000 13.5
2166 2015-09-22 00:00:00.000 28.5
3542 2015-11-05 00:00:00.000 13.5
3543 2015-12-18 00:00:00.000 6
2445 2015-12-25 00:00:00.000 5.7
4096 2015-12-31 00:00:00.000 7.5
2446 2016-01-01 00:00:00.000 5.7
4287 2016-02-11 00:00:00.000 13.5
4288 2016-02-18 00:00:00.000 13.5
4492 2016-03-02 00:00:00.000 19.7
2447 2016-03-25 00:00:00.000 5.7
I am using a stored procedure which adds up the Days then subtracts it from a fixed value stored in a variable.
The total in the table is 245 and the variable is set to 245 so I should get a value of 0 when subtracting the two. However, I am getting a value of 5.6843418860808E-14 instead. I cant figure out why this is the case and I have gone and re entered each number in the table but I still get the same result.
This is my sql statement that I am using to calculate the result:
Declare #AL_Taken as float
Declare #AL_Remaining as float
Declare #EntitledLeave as float
Set #EntitledLeave=245
set #AL_Taken= (select sum(Days) from tblALMain)
Set #AL_Remaining=#EntitledLeave-#AL_Taken
Select #EntitledLeave, #AL_Taken, #AL_Remaining
The select returns the following:
245, 245, 5.6843418860808E-14
Can anyone suggest why I am getting this number when I should be getting 0?
Thanks for the help
Rob
I changed the data type to Decimal as Tab Allenman suggested and this resolved my issue. I still dont understand why I didnt get zero when using float as all the values added up to 245 exactly (I even re-entered the values manually) and 245 - 245 should have given me 0.
Thanks again for all the comments and explanations.
Rob

Select values based on another cell's values, then calculate a statistic from those cells and put it in a specific cell

I have is a dataset of daily stream runoff values for the past 11 years. It looks like this:
ID Year DD Apr May Jun Jul Aug Sep Oct
08HF004 2000 1 26.5 37.6 18.3 12.3 8.35 5.19 7.98
08HF004 2000 2 28.8 25.8 19.3 10.4 6.86 4.61 5.86
08HF004 2000 3 34.7 22.8 25.9 9.32 5.82 4.07 4.71
08HF004 2000 4 29.7 19.4 33.8 9.16 5.5 3.61 4.01
08HF004 2000 5 19.9 17.5 38.6 9.01 5.39 3.32 3.53
08HF004 2000 6 15 14.6 33.1 9.04 5.22 3.32 3.2
08HF004 2000 7 11.6 14.1 27 10.3 4.83 4.55 2.96
...and so forth for 400+ more lines. What I want to do is use VBA to select all the values from each month (April 2000, May 2000, etc) and figure out the average and standard deviation from each month and send them to a cell in the worksheet, or a cell in another worksheet, or, ideally, a new workbook in the directory I can just call "results".
I suggest a PivotTable (one month per table) - Year for ROWS and say April for VALUES (once as Average of and once as StdDev of or StdDevp of).
Or you might 'flatten' the data (eg as shown here) and use different views of a single PivotTable:

Index (columns [0]) have duplicate values

I have the following data:
Start Time=2012-04-12 16:13:09
Finish Time=2012-11-30 13:31:08
Sample Period=01:00:00
As a CSV file:
Date(yyyy-mm-dd) Time(hh:mm:ss) Celsius (C)
2012-04-12 16:13:09 20.6
2012-04-12 17:13:09 20.6
2012-04-12 18:13:09 20.6
2012-04-12 19:13:09 20.6
2012-04-12 20:13:09 20.6
2012-04-12 21:13:09 20.6
2012-04-12 22:13:09 20.6
2012-04-12 23:13:09 20.6
and now I want to read-in data by pandas like this:
df=read_csv('mmf0401.txt',skiprows=(0,5),parse_dates=[[0,1]],index_col=0)
Unfortunately, it raises an exception Index (columns [0]) have duplicate values ['2012-04-12']
I don't know why, how can I correct this?