pandas extract year from datetime: df[‘year’] = df[‘date’].year is not working

If you’re running a recent-ish version of pandas then you can use the datetime accessor dt to access the datetime components: In [6]: df[‘date’] = pd.to_datetime(df[‘date’]) df[‘year’], df[‘month’] = df[‘date’].dt.year, df[‘date’].dt.month df Out[6]: date Count year month 0 2010-06-30 525 2010 6 1 2010-07-30 136 2010 7 2 2010-08-31 125 2010 8 3 2010-09-30 84 … Read more

datetime and timezone conversion with pytz – mind blowing behaviour

The documentation states “Unfortunately using the tzinfo argument of the standard datetime constructors ‘does not work’ with pytz for many timezones.” The code: t = datetime( 2013, 5, 11, hour=11, minute=0, tzinfo=pytz.timezone(‘Europe/Warsaw’) ) doesn’t work according to this, instead you should use the localize method: t = pytz.timezone(‘Europe/Warsaw’).localize( datetime(2013, 5, 11, hour=11, minute=0))

Get Start and End Days for a Given Week in PHP

I would take advantange of PHP’s strtotime awesomeness: function x_week_range(&$start_date, &$end_date, $date) { $ts = strtotime($date); $start = (date(‘w’, $ts) == 0) ? $ts : strtotime(‘last sunday’, $ts); $start_date = date(‘Y-m-d’, $start); $end_date = date(‘Y-m-d’, strtotime(‘next saturday’, $start)); } Tested on the data you provided and it works. I don’t particularly like the whole reference … Read more

Format .NET DateTime “Day” with no leading zero

To indicate that this is a custom format specifier (in contrast to a standard format specifier), it must be two characters long. This can be accomplished by adding a space (which will show up in the output), or by including a percent sign before the single letter, like this: string result = myDate.ToString(“%d”); See documentation

Is there a standard date/time class in C++?

Not part of STL but well known library is boost. I would go the way of using boost::date. Here are some examples: If you did not try out boost yet I encourage you to do so as it saves you from a lot of nasty issues, as it masks most OS dependent things like … Read more