pd.Timestamp changing month and day field when day <=12 [duplicate] - python-3.x

This question already has answers here:
pd.to_datetime is getting half my dates with flipped day / months
(2 answers)
Closed 3 years ago.
I'm doing some data analysis using Pandas in a Jupyter notebook and analysing minute by minute data for a 2 year period. There's about 740,000 rows of data pulled in from a csv file (pd.read_csv('file location'))
Format of date_time: "dd/mm/yyyy hh:mm" - i.e. 11/01/2017 21:52
Here's something weird I've found, when day<=12, the day and month switch, when the day is 13 and above the format is correct (when put through pd.Timestamp)
An example:
pd.Timestamp("13/02/2018 02:26")
--> Timestamp('2018-02-13 02:26:00')
As you'd expect. Now taking it to 11th February:
pd.Timestamp("12/02/2018 02:26")
--> Timestamp('2018-12-02 02:26:00')
Really unsure why this happens, would really appreciate some help on how I can avoid this happening.
Thank you!

Check with to_datetime and dayfirst
pd.to_datetime("11/02/2018 02:26",dayfirst=True)
Out[22]: Timestamp('2018-02-11 02:26:00')

Related

How to convert minutes into (YY MM DD HH:mm:ss) in excel?

I'm using manipulating time in excel.
I have the date in minutes I want to convert it into the format.
YY MM DD HH mm
where:
mm:Minutes
HH:Hours
DD:Days
MM:month
YY: year
Im using the following logic:
For 124 minutes I have
124/60= 2 hours
124%60 = 4 minutes
and so on for the days and the months and the years
The units Im using are:
mm-> HH /24
HH-> MM /30
MM-> YY /12
But something is really wrong:
Does anyone have an idea what was happening?
Thanks a lot, guys
Your calculations are "really wrong" because in real life, months have anywhere between 28 and 31 days, so you cannot represent a large number of minutes as a "date" that uses variable measures for years and months.
On the assumption that as units of measure you want to use ...
Year = 360 days
month - 30 days
... you can calculate as follows:
That's also what your calculation shows. If something is "really wrong" with that, it's probably your expectation, which is based on the traditional 365.25 day year.
Maybe you want to edit your question and explain what you expect as the "really correct" result.
In Excel dates are fractional number of days from 31-Dec-1899 (date 0 is formatted as 0/1/1900). So why don't you just add your minutes (after dividing by (24*60) to convert to days) to the earliest valid date in EXCEL (1900-01-01) and use the TEXT function to format:
=DATEVALUE("1900-01-01")+(21/24/60)-1
The bold part is your minutes.
This is now a proper EXCEL date; you can use TEXT function to format it into any way you like, or you can use DAY(), HOUR(), etc. functions to extract parts.
Having said that I don't know what your minutes represent; What date does Full Time in Minutes:21 correspond to?

Date to Weeknum in Access/Excel/VBA [duplicate]

This question already has answers here:
MS Access query, how to use SQL to group single dates into weeks
(2 answers)
VBA Convert date to week number
(8 answers)
Closed 2 years ago.
I have an access-file, that I imported in excel as a PivotTable. I want the possibility to sort the data in a slicer as Week numbers. I have grouped and set days to 7, but that only gives me 2020-01-01 - 2020-01-07 for example.
Should I convert the weeknum's already in Access? And then, how do I do that?
Please explain it all, even where to paste the code and how to implement it in Access.
Thank you.

Calculate past 12 and 6 month Average

The excel user will export the data from an online website to excel (12 months data), so the data will be all the time different.
I need the past 6 months and 12 months average (However, the calculation need to use the months I have in the data, and sometimes there will be less then 6 or 12 months), but I still need to get the average and frequency for it , however, I am not sure how to get it.
I am trying to write a code, but it is not complete and is not working as well, I don't get an error; it just doesn't work.
I am open for Excel formulas as well, the problem may be the last row and that it need to use the data I have to calculate and not 6 and 12 months full.
PS: I post a similar question on https://www.ozgrid.com/forum/index.php?thread/1227312-dynamic-way-to-calculate-the-last-6-months-average/
Will This formulas work for you?
Average for last 6 months:
=AVERAGEIF(A:A;">="&EDATE(MAX(A:A);-6);B:B)
Frequency for last 6 months:
=IF(MONTH(MAX(A:A)-MIN(A:A))>=6;COUNTIF(A:A;">="&EDATE(MAX(A:A);-6))/6;COUNTIF(A:A;">="&MIN(A:A))/MONTH(MAX(A:A)-MIN(A:A)))
Average for last 12 months:
=AVERAGEIF(A:A;">="&EDATE(MAX(A:A);-12);B:B)
Frequency for last 12 months:
=IF(MONTH(MAX(A:A)-MIN(A:A))>=12;COUNTIF(A:A;">="&EDATE(MAX(A:A);-12))/12;COUNTIF(A:A;">="&MIN(A:A))/MONTH(MAX(A:A)-MIN(A:A)))

Convert minutes into 100ths of an hour using Excel [duplicate]

This question already has an answer here:
Converting large time format to decimal in excel
(1 answer)
Closed 5 years ago.
I have a function =TIME(8,30,0)-(C2-B2) that work out my timesheet for me but I also need the resulting elapsed time value to be converted into hours and 100ths of an hour.
e.g. 1:45:00 would be 1.75.
Multiplying the result by 100 and dividing by 60 does not seem to give the desired result.
Time in Excel is a decimal. so one hour is 1/24th of a full day. So 1/24.
Multiply the time by 24 and you will get your decimal:
=A1*24

Convert fraction of day to POSIX times in R [duplicate]

This question already has answers here:
Convert decimal day to HH:MM
(3 answers)
Closed 3 years ago.
I have a dataset that encodes a date-time into two separate variables. Normally, I'd just paste them together inside of an as.POSIXct and carry on. However, the date is provided as a string, and the time of day as a fraction of 24 hours - e.g., 12pm is 0.5, 9:30am is 0.1458333, etc.
It doesn't seem all that tricky to convert the fractional days into clock hours, but I'd prefer to use a pre-existing function if possible. Does something like that exist in base R? A package?
If it's any use, this is an Excel (xlsx) time field imported into R through RODBC.
EDIT
Oddly enough, upon revisiting this problem, the times are now read in as POSIXct. Not sure what to make of that.
The R News 4/1 Help Desk article has a section on reading Excel dates in R.
POSIXct values are simply the number of seconds since midnight GMT 1970-01-01. (So you need to pay attention to your offset from UTC.) You can use the date part and add the number of days times 24*3600 (as.Date(dtval) to your time value * 24*3600. Gabor pointed to the article in R News (which he wrote, thank you, Gabor.)
You didn't give an example of the string. If you are getting your date as a string, then as.Date(strDate) will convert a variable "strDate" to Date class when it is in either "YYYY-MM-DD" or "YYYY/MM/DD" format. Otherwise the formatting codes are on the ?strptime page.
Once you have a POSIXct-classed variable you can just add the number of seconds. This example add 30 minutes to midnight today Feb 1, 2011 (in my time zone which is UTC-5):
> as.POSIXct(as.Date("2011-02-01")) +30*60
[1] "2011-01-31 19:30:00 EST"
And this is your time value added to midnight my time:
> as.POSIXct(as.Date("2011-02-01 00:00", tzone="UTC"))+3600*5 + 3600*24*timeval
[1] "2011-02-01 03:29:59 EST"

Resources