I have two columns, fromdate and todate, in a dataframe. I add a new column, diff, to find the difference between the two dates using I get the diff column, but it contains days, when there’s more than 24 hours. How do I convert my results to only hours and minutes (i.e. days are converted to hours)? Answer Pandas timestamp
Tag: datetime
Convert datetime to Unix timestamp and convert it back in python
I have dt = datetime(2013,9,1,11), and I would like to get a Unix timestamp of this datetime object. When I do (dt – datetime(1970,1,1)).total_seconds() I got the timestamp 1378033200. When converting it back using datetime.fromtimestamp I got datetime.datetime(2013, 9, 1, 6, 0). The hour doesn’t match. What did I miss here? Answer What you missed here is timezones. Presumably you’ve
elegant way of convert a numpy array containing datetime.timedelta into seconds in python 2.7
I have a numpy array called dt. Each element is of type datetime.timedelta. For example: how can I convert dt into the array dt_sec which contains only seconds without looping? my current solution (which works, but I don’t like it) is: I tried to use dt.total_seconds() but of course it didn’t work. any idea on how to avoid this loop?
Convert pandas timezone-aware DateTimeIndex to naive timestamp, but in certain timezone
You can use the function tz_localize to make a Timestamp or DateTimeIndex timezone aware, but how can you do the opposite: how can you convert a timezone aware Timestamp to a naive one, while preserving its timezone? An example: I could remove the timezone by setting it to None, but then the result is converted to UTC (12 o’clock became
How to monkeypatch builtin function datetime.datetime.now?
I’d like to make sure that datetime.datetime.now() returns a specific datetime for testing purposes, How do I do this? I’ve tried with pytest’s monkeypatch But this gives me the error TypeError: can’t set attributes of built-in/extension type ‘datetime.datetime’ Answer As the error tells you, you can’t monkeypatch the attributes of many extension types implemented in C. (Other Python implementations may
Determine start and end time of current day (UTC -> EST -> UTC) ; Python
I am storing all my times in UTC and my system is set to UTC (though I am in EST). I have dates stored as: However, I would like to select information based off today for EST, so I am attempting to: Get current time as UTC and change to EST Next I want to get the start time for
python pandas extract unique dates from time series
I have a DataFrame which contains a lot of intraday data, the DataFrame has several days of data, dates are not continuous. How can I extract the unique date in the datetime format from the above DataFrame? To have result like [2012-10-08, 2012-10-10] Answer If you have a Series like: where each object is a Timestamp: you can get only
A faster strptime?
I have code which reads vast numbers of dates in ‘YYYY-MM-DD’ format. Parsing all these dates, so that it can add one, two, or three days then write back in the same format is slowing things down quite considerably. Any suggestions how to speed it up a bit (or a lot)? Answer Python 3.7+: fromisoformat() Since Python 3.7, the datetime
How can I make a python numpy arange of datetime
I have some input data, with timestamps in the input file in the form of hours from the date time specified in the filename. This is a bit useless, so I need to convert it to python datetime.datetime objects, and then put it in a numpy array. I could write a for loop, but I’d like to do something like:
Weird timezone issue with pytz
A seven hour and 37 minute offset? This is a little strange, does anyone experience the same issue? In fact I’m getting different behavior between Answer Time zones and offsets change over the years. The default zone name and offset delivered when pytz creates a timezone object are the earliest ones available for that zone, and sometimes they can seem