Is there a way to specify a datetime.date without a day like that:
datetime.date(year=1900, month=1, day=None)
I have a dataset with not full specified dates (sometimes only the year and the month is in it). I want to reprepsent that with a datetime.date without doing tricks.
"Beautiful is better than ugly. Explicit is better than implicit..." - Python's Philosophy
You cannot do that by built-in datetime.date. Python datetime.date doesn't have function signature as not to put the day value. Perhaps it is due to the fact that date without day (of month) is naturally an incomplete date in real life.
Additionally, since day input is seen as Integer thus it must have value. And the default integer value as 0 will cause day representation error (albeit the internal mechanism for counting datetime might work around with it), as our day in real life starts with 1. In short, datetime.date has done a pretty good job (in terms of safe of use) - consistent with its "Explicit is better than implicit philosophy" - by not letting the user to call it without specifying day (that is: by hinting what is required in the function signature as what every good programmer would do).
But, you could create your own function wrapper whenever you feel it is annoying or unnecessary too.
Edit:
or using Python's own wrapper:
monthdate = functools.partial(datetime.date, day=1) #edit by ShadowRanger
To me, what seems to be the simplest practice would be to use the current built-in with the value of day as 1.
datetime.date(1900, 1, 1)
It is a very short ,1 to be added
datetime.date represents a day in Gregorian calendar. It is immutable and therefore all values must be known at the instant it is created. You can't omit the day if you use the constructor explicitly.
I have a dataset with not full specified dates
datetime.strptime() provides the default values if necessary:
>>> from datetime import datetime
>>> datetime.strptime('2016-02', '%Y-%m').date()
datetime.date(2016, 2, 1)
Related
def format_date_joined(date):
return
So the function is supposed to take in a date as an argument then return it with the month, year format. I'm not sure exactly how to do that as I don't even know what format the date would be entered.
Obviously you have so far spent zero time with the datetime Python built-in class ...
It is reasonable to assume that the date parameter to your function already is an object of that class. Therefore, you simply need to return the value that is produced by an appropriate call to its strftime method. (The entire purpose of this function being "to conceal this 'necessary bit of business' from everyone else.)
I am using api it require date as this format
2020-03-01T00:00:00Z
I googled around and couldn't under stand what the T Z means.
For Now I made this string with this code by python
dt_now.strftime('%Y-%m-%dT%H:%M:%SZ')
However it looks a bit awkward and I am not sure if it is correct.
Is there any good way for python datetime??
This looks like ISO 8601 time format. The T stands for time and is used as separator, while Z determines time offset and stands for Zulu which is commonly used, military originated, name alias for UTC+0 offset. For other offsets you need to specify it as HH:MM, with + or - respectively. So the Z is therefore equivalent of writing +00:00.
See https://en.wikipedia.org/wiki/ISO_8601 for more info.
I am having troubles understanding the difference between a PeriodIndex and a DateTimeIndex, and when to use which. In particular, it always seemed to be more natural to me to use Periods as opposed to Timestamps, but recently I discovered that Timestamps seem to provide the same indexing capability, can be used with the timegrouper and also work better with Matplotlib's date functionalities. So I am wondering if there is every a reason to use Periods (a PeriodIndex)?
Periods can be use to check if a specific event occurs within a certain period. Basically a Period represents an interval while a Timestamp represents a point in time.
# For example, this will return True since the period is 1Day. This test cannot be done with a Timestamp.
p = pd.Period('2017-06-13')
test = pd.Timestamp('2017-06-13 22:11')
p.start_time < test < p.end_time
I believe the simplest reason for ones to use Periods/Timestamps is whether attributes from a Period and a Timestamp are needed for his/her code.
This question already has answers here:
Convert weird Python date format to readable date
(2 answers)
Closed 7 years ago.
I'm importing data from an Excel spreadsheet into python. My dates are coming through in a bizarre format of which I am not familiar and cannot parse.
in excel: (7/31/2015)
42216
after I import it:
u'/Date(1438318800000-0500)/'
Two questions:
what format is this and how might I parse it into something more intuitive and easier to read?
is there a robust, swiss-army-knife-esque way to convert dates without specifying input format?
Timezones necessarily make this more complex, so let's ignore them...
As #SteJ remarked, what you get is (close to) the time in seconds since 1st January 1970. Here's a Wikipedia article how that's normally used. Oddly, the string you get seems to have a timezone (-0500, EST in North America) attached. Makes no sense if it's properly UNIX time (which is always in UTC), but we'll pass on that...
Assuming you can get it reduced to a number (sans timezone) the conversion into something sensible in Python is really straight-forward (note the reduction in precision; your original number is the number of milliseconds since the epoch, rather than the standard number of seconds from the epoch):
from datetime import datetime
time_stamp = 1438318800
time_stamp_dt = datetime.fromtimestamp(time_stamp)
You can then get time_stamp_dt into any format you think best using strftime, e.g., time_stamp_dt.strftime('%m/%d/%Y'), which pretty much gives you what you started with.
Now, assuming that the format of the string you provided is fairly regular, we can extract the relevant time quite simply like this:
s = '/Date(1438318800000-0500)/'
time_stamp = int(s[6:16])
I have this timestamp value being return by a web service "2014-09-12T19:34:29Z"
I know that it means timezone, but what exactly does it mean?
And I am trying to mock this web service, so is there a way to generate this timestamp using strftime in python?
Sorry if this is painfully obvious, but Google was not very helpful and neither was the strftime() reference page.
I am currently using this :
x.strftime("%Y-%m-%dT%H:%M:%S%Z")
'2015-03-26T10:58:51'
The T doesn't really stand for anything. It is just the separator that the ISO 8601 combined date-time format requires. You can read it as an abbreviation for Time.
The Z stands for the Zero timezone, as it is offset by 0 from the Coordinated Universal Time (UTC).
Both characters are just static letters in the format, which is why they are not documented by the datetime.strftime() method. You could have used Q or M or Monty Python and the method would have returned them unchanged as well; the method only looks for patterns starting with % to replace those with information from the datetime object.